【问题标题】:Implementing IsDirty in WPF MVVM Observable collection在 WPF MVVM Observable 集合中实现 IsDirty
【发布时间】:2016-12-08 00:55:37
【问题描述】:

我有一个模型:

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

【问题讨论】:

  • 我可能错了,但是要绑定那个东西,它已经被实例化了,不是吗?所以也许更准确地解释一下你自己
  • 加载 Config.Schedules 后,为什么不直接重置呢? foreach(Config.Schedules 中的 var sched) sched.IsDirty = false;如果您真的不希望将 IsDirty 设置为 true,则可以将 IsDirty 更改为可为空的布尔值,并且如果 IsDirty 为空,则不要在属性设置器中更新 IsDirty。您仍然需要在某些时候设置 IsDirty = false 以表明您希望 IsDirty 现在在属性设置器中更新。
  • 便宜又脏,使用加载后设置的布尔标志。没有“解决方案”。
  • @J.H 把它写成答案,这样我就可以给你功劳了。

标签: c# wpf mvvm


【解决方案1】:

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

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

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

【讨论】:

  • 感谢您的回答。我在 Window_Loaded 事件中添加了一个 foreach。效果很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-04
  • 2012-04-27
  • 2012-12-21
  • 2017-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多