2013-03-13 57 views
0

我想問你,當我更改源代碼時,如何反映View上的更改。這是我的代碼。 MODEL:MVVM NotifyPropertyChanged

public class ExecutionMode 
{ 
    private List<DayItem> _dayItems = new List<DayItem>(); 

    public enum TypeMode 
    { 
     None, 
     Once, 
     Daily, 
     Weekly 
    } 

    public TypeMode Mode { get; set; } 
    public DateTime? ExecutionTime { get; set; } 
    public DateTime? ExecutionDate { get; set; } 
    public List<DayItem> DayItems { get { return _dayItems; } set { _dayItems = value; } } 
} 
public class ProfileRecord 
    { 
     private ExecutionMode _mode = new ExecutionMode(); 
     public ExecutionMode ExecutionMode { get { return _mode; } set { _mode = value; } } 
    } 

視圖模型

public class NewProfileViewModel : INotifyPropertyChanged 
    {private ProfileRecord _record = new ProfileRecord(); 
public ProfileRecord Record { get{return _record; } set{_record=value; OnPropertyChanged("Record")}} 

XAML:

<toolkit:TimePicker Header="Time of execution:" Margin="12,0,70,0" Value="{Binding ProfileRecord.ExecutionMode.ExecutionTime, Mode=TwoWay}" Visibility="{Binding ElementName=lpickerExecutionModes, Path=SelectedItem, Converter={StaticResource VisibilityConvert}, ConverterParameter=Once|Daily|Weekly}" /> 

當我在代碼Record.ExecutionTime設置某處=時間不上查看反映。所以我問。我是否也應該在模型中實現NotifyPropertyChanged? 感謝

回答

1

有解決問題的2種方式:

  1. ViewModel在模型類上實現INotifyPropertyChanged

  2. 地圖從模型對象的屬性的所有必要的屬性和對這些實施INotifyPropertyChanged屬性。

我更喜歡第一種方法,尤其是當模型是非常巨大的,有很多的屬性,所有這些屬性在視圖中使用。

+0

所以我應該在類ProfileRecord中實現INotifyPropertyChanged,並將其用於公共ExecutionMode ExecutionMode {get {return _mode; } set {_mode = value; }}?對? – JuP 2013-03-13 08:58:33

+0

無論它真的屬於哪個類,你都必須在每個可以從'View'綁定的屬性上實現'INotifyPropertyChanged'。 – MarcinJuraszek 2013-03-13 09:00:45

+0

@JurajP:是的,但您剛剛粘貼的ExMode {get; set}的實際實現應該實際上包含在其setter中的raise-INPC通知:) – quetzalcoatl 2013-03-13 09:01:52