在WPF MVVM Observable集合中实现IsDirty

时间:2016-08-02 15:14:25

标签: c# wpf mvvm

我有一个模特:

namespace CCBDPlayer.Models
{
public class Schedule : DependencyObject, IEquatable<Schedule>, INotifyPropertyChanged
{
    private DateTime _scheduledStart;
    private DateTime _scheduledEnd;
    private bool _enabled;
    private string _url;
    private bool _isDirty;

    public event PropertyChangedEventHandler PropertyChanged;

    public Schedule() { }

    public static readonly DependencyProperty IsDirtyProperty = 
        DependencyProperty.Register("IsDirty", typeof(Boolean),typeof(Schedule));
    public DateTime ScheduledStart
    {
        get { return _scheduledStart; }
        set
        {
            _scheduledStart = value;
            OnPropertyChanged(new PropertyChangedEventArgs("ScheduledStart"));
        }
    }
    public DateTime ScheduledEnd
    {
        get { return _scheduledEnd; }
        set
        {
            if (value < ScheduledStart)
            {
                throw new ArgumentException("Scheduled End cannot be earlier than Scheduled Start.");
            }
            else
            {
                _scheduledEnd = value;
                OnPropertyChanged(new PropertyChangedEventArgs("ScheduledEnd"));
            }
        }
    }
    public bool Enabled
    {
        get { return _enabled; }
        set
        {
            _enabled = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Enabled"));
            IsDirty = true;
        }
    }
    public string Url
    {
        get { return _url; }
        set
        {
            _url = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Url"));
            IsDirty = true;
        }
    }

    [XmlIgnoreAttribute]
    public bool IsDirty
    {
        get { return (bool)GetValue(IsDirtyProperty); }
        set { SetValue(IsDirtyProperty, value); }
    }

    public bool Equals(Schedule other)
    {
        if(this.ScheduledStart == other.ScheduledStart && this.ScheduledEnd == other.ScheduledEnd 
            && this.Enabled == other.Enabled && this.Url == other.Url)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, e);
        }
    }
}

}

此模型用于我的ViewModel中存在的ObservableCollection。 ObservableCollection绑定到我的视图中的ItemsControl:

            <ItemsControl ItemsSource="{Binding Config.Schedules}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border x:Name="ScheduleBorder" BorderBrush="Black" BorderThickness="1" Margin="5,5" VerticalAlignment="Top">
                        <Grid VerticalAlignment="Top">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="4*" />
                                <RowDefinition Height="2*" />
                            </Grid.RowDefinitions>
                            <Grid Grid.Row="0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="2*" />
                                    <ColumnDefinition Width="3*" />
                                    <ColumnDefinition Width="1*" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition />
                                    <RowDefinition />
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <Label Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Content="Scheduled Start" VerticalAlignment="Top"/>
                                <xctk:DateTimePicker Grid.Column="1" Grid.Row="0" Value="{Binding ScheduledStart}" Margin="0,2" VerticalAlignment="Center"  />
                                <Label Grid.Column="0" Grid.Row="1" HorizontalAlignment="Left" Content="Scheduled End" />
                                <xctk:DateTimePicker Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Value="{Binding ScheduledEnd}" Margin="0.2" />
                                <Button Grid.Row="0" Grid.Column="2" Margin="5,5" Background="White" VerticalAlignment="Top" Width="15" Height="15" 
                                        Command="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=DataContext.RemoveScheduleCommand}" CommandParameter="{Binding}">
                                    <Image Source="Images/delete-button.png"/>
                                </Button>
                                <Label Grid.Column="0" Grid.Row="2" HorizontalAlignment="Left" Content="Url" VerticalAlignment="Top"/>
                                <TextBox Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Top" Text="{Binding Url}" Width="Auto"/>
                            </Grid>
                            <Grid Grid.Row="1">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="3*"/>
                                    <ColumnDefinition Width="1*" />
                                </Grid.ColumnDefinitions>
                                <CheckBox Content="Enable" Margin="5" IsChecked="{Binding Enabled}"/>
                                <Button Grid.Column="1" HorizontalAlignment="Right" Width="65" Content="Save" Margin="0, 2"/>
                            </Grid>
                        </Grid>
                    </Border>
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding Path=IsDirty}" Value="true">
                            <Setter Property="Background" TargetName="ScheduleBorder" Value="Yellow"/>
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

我需要能够在模型实例的初始化之后才设置IsDirty。有什么建议吗?

更新

如果实例是“脏”,我有DataTrigger将模板的背景设置为黄色。如上所述,如果我只是添加     IsDirty = true 对于属性设置器,模板将始终具有黄色背景。我需要一种方法让模型忽略属性上的第一个初始化值。

1 个答案:

答案 0 :(得分:0)

加载Config.Schedules后,为什么不重置它?

foreach (var sched in Config.Schedules)
     sched.IsDirty = false;

如果你真的不想将IsDirty设置为true,那么可能会将IsDirty更改为可以为空的bool,如果IsDirty为null,则不要在属性设置器中更新IsDirty。您仍然必须在某个时刻设置IsDirty = false以表示您希望现在在属性设置器中更新IsDirty