wpf2017. 8. 22. 12:41

물론 ViewModel에서는 MVVM Light 라이브러리에 있는 NotifyProperty가 알아서 해주겠지만


Model 같은경우는 자기가 만들면 된다.


에를 들면 ObservableCollection에서 어떤 Property를 수정하였을때 즉각적으로 바인딩된 UI에 적용하고 싶다면


Model에 INotifyPropertyChanged 인터페이스를 구축하면 된다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    public class OrderedMenuItem : INotifyPropertyChanged
    {
        public int Index { get; set; }
 
        private int _orderCount;
 
        public int OrderCount
        {
            get { return _orderCount; }
            set
            {
                if (_orderCount != value)
                {
                    _orderCount = value;
                    NotifyPropertyChanged("OrderCount");
                }
            }
        }
 
        public event PropertyChangedEventHandler PropertyChanged;
 
        public void NotifyPropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(thisnew PropertyChangedEventArgs(propName));
        }
    }


참고 : http://www.wpf-tutorial.com/data-binding/responding-to-changes/


Posted by 동동(이재동)
wpf2017. 8. 8. 16:18

예를들어 View의 UserControl의 DataContext에 데이터를 주입하고

 <local:CardCancelControl DataContext="{Binding CardCancelData}" />


이런식으로...

유저컨트롤에서 버튼 커맨드를 넣었을때


 <Button Content="확인" Command="{Binding DataContext.CardCancelCommand}"  CommandParameter="Cancel" />


이런식으로 넣으면 안된다. datacontext안에 서 command를 호출하기 때문이다


이렇게 할려면 상위에서 DataContext를 빼야 작동한다.


그러면 상위에서 호출되게 할려면 어떻게 해야할까.


 <Button Content="확인" Command="{Binding Path=DataContext.CardCancelCommand,

                        RelativeSource={RelativeSource Mode=FindAncestor,

                        AncestorType={x:Type local:PopupControl}}}" CommandParameter="Cancel" />


이렇게 하면 된다.

참고 : https://stackoverflow.com/questions/3404707/access-parent-datacontext-from-datatemplate


Posted by 동동(이재동)
wpf2017. 8. 8. 15:38

MvvmLight를 사용한다는 기준으로 작성한다.


1
2
3
4
        private void ExcuteSettingCommand()
        {
            MessengerInstance.Send(SettingEnum.OpenSettingPopup);
        }


MessngerInstance를 이용하여

일단 보내는 값을 object형태 (여기서는 enum)로 보낸다.


받는 ViewModel에서는


1
2
3
4
5
6
7
8
9
  public PopupViewModel()
        {
            MessengerInstance.Register<SettingEnum>(this, c => SettingExcute(c));
        }
 
        public void SettingExcute(SettingEnum result)
        {
            //some code
        }


이런식으로 등록하면 통신이 된다.

Posted by 동동(이재동)
wpf2017. 8. 7. 11:29

일반적으로 Grid라든지 컨트롤들은 자동으로 해상도에 맞게 조절해주지만


이미지는 크기가 정해져 있어서 이미지 크기가 유동적으로 변하지 않는다.


그래서 만약 Grid를 사용할때 퍼센트를 이용해서 분할하였다면 이미지를 사용한 Row나 column은 이미지가 짤릴경우가 있다.


이럴때는 ViewBox로 그 Row를 감싸면 깔끔해진다.



Posted by 동동(이재동)