c# - 在 WPF MVVM Observable 集合中实现 IsDirty

标签 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 绑定(bind)到我的 View 中的 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 将模板的背景设置为黄色。就目前而言,如果我只是添加
脏=真
到属性 setter ,那么模板将始终具有黄色背景。我需要一种方法让模型忽略属性上的第一个初始化值。

最佳答案

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

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

如果您真的不希望将 IsDirty 设置为 true,则可以将 IsDirty 更改为可为空的 bool 值,并且如果 IsDirty 为空,则不要在属性 setter 中更新 IsDirty。您仍然需要在某些时候设置 IsDirty = false 以表明您希望 IsDirty 现在在属性 setter 中更新

关于c# - 在 WPF MVVM Observable 集合中实现 IsDirty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38724339/

相关文章:

c# - 在没有 MVVM 框架的情况下创建子窗口

c# - MVVM、WPF、C# 中的拇指拖动

c# - 数据 throttle C#

c# - 如何检测我的应用程序中的用户不活动(Windows Mobile,C#)

c# - 响应式(Reactive)编程中流之间的循环依赖

c# - wpf mvvm 是否可以有一个带有对象列表的 ListView ,在它下面有一个包含所选项目的 View

c# - 如何在我的 WPF 应用程序中使用标准 Windows 警告/错误图标?

android - 处理 recyclerview 适配器(Kotlin)中的按钮点击?

c# - 如何使用 MVVM、C# 和无代码隐藏将 Row 的文本动态设置为粗体?

c# - C# 和 Java 中的字节处理方式相同吗?