MVVM NotifyPropertyChanged

时间:2013-03-13 08:39:43

标签: c# mvvm

我想问你,当我更改源代码时,如何反映View的更改。这是我的代码。的 MODEL:

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 = Time中设置某个地方时,它不反映在View上。所以我问。我是否应该在Model中实现NotifyPropertyChanged? 感谢

1 个答案:

答案 0 :(得分:1)

有两种方法可以解决问题:

  1. 在您的Model类上实施INotifyPropertyChanged

  2. 将Model对象中的所有必要属性映射为ViewModel中的属性,并在这些属性上实现INotifyPropertyChanged

  3. 我更喜欢第一种方法,特别是当模型非常庞大,具有很多属性时,所有这些属性都在View中使用。