JuP
JuP

Reputation: 535

MVVM NotifyPropertyChanged

I want to ask you, how can I reflect changes on View, when I changed source. Here's my code. 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; } }
    }

ViewModel

    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}" />

When I set somewhere in code Record.ExecutionTime = Time it doesn't reflect on View. So I'm asking. Should I implement NotifyPropertyChanged in Model too? Thanks

Upvotes: 0

Views: 196

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125650

There are 2 ways of solving the problem:

  1. Implement INotifyPropertyChanged on your Model class

  2. Map all necessary properties from Model object as a properties within ViewModel and implement INotifyPropertyChanged on those properties.

I prefer the first approach, especially when model is really huge, has a lot of properties, and all these properties are used in View.

Upvotes: 1

Related Questions