在WPF MVVM可观察集合中实现IsDirty

亚伦·拉姆福德(Aaron Rumford)

我有一个模型:

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添加到属性设置器中,则模板将始终具有黄色背景。我需要一种使模型忽略属性上的第一个初始化值的方法。

JH

加载Config.Schedules之后,为什么不重设它呢?

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

如果您确实不希望将IsDirty设置为true,则可以将IsDirty更改为可为null的布尔值,并且如果IsDirty为null,则不要在属性设置器中更新IsDirty。您仍然必须在某些时候设置IsDirty = false,以表明您现在希望在属性设置器中更新IsDirty。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从可观察的集合中动态删除项目-WPF

来自分类Dev

WPF可观察的集合,动态类型

来自分类Dev

C# WPF 如何在以编程方式更新数据网格后更新可观察集合 - 没有 MVVM

来自分类Dev

由c#wpf中的类类型的可观察集合填充的过滤器lisbox

来自分类Dev

WPF双向数据绑定到可观察集合中的自定义数据类型

来自分类Dev

WPF Listview使用ListView Item中的按钮更改可观察的集合属性

来自分类Dev

由c#wpf中的类类型的可观察集合填充的过滤器lisbox

来自分类Dev

嵌入式Winform图表可以绑定到WPF中的可观察集合吗?

来自分类Dev

如何将ListboxItem绑定为WPF中的可观察集合,并在顶部插入新记录

来自分类Dev

刷新WPF Datagrid是否不绑定到可观察的集合?

来自分类Dev

在WPF MVVM中处理模型中的集合

来自分类Dev

在WPF MVVM中处理模型中的集合

来自分类Dev

如何在可观察的集合WPF中设置Canvas.Left和Canvas.Top-Windows Phone 8.1

来自分类Dev

WPF将组合框绑定到LINQ填充的可观察集合

来自分类Dev

WPF TreeView的C#可观察集合LDAP路径子级

来自分类Dev

WPF将项目添加到可观察的集合并打印。返回对象参考错误

来自分类Dev

如何将WPF TabControl ContentTemplate绑定到不同ViewModel的可观察集合

来自分类Dev

WPF将datagrid绑定到字符串可观察的集合

来自分类Dev

将可观察的集合绑定到MVVM中的包装面板

来自分类Dev

在mvvm light中获取可观察的排序集合

来自分类Dev

MVVM跨WPF区域实现

来自分类Dev

在WPF应用程序的mvvm中实现导航的最佳方法

来自分类Dev

MVVM绑定可观察的集合以查看吗?

来自分类Dev

使用MVVM保存/加载可观察的集合

来自分类Dev

如何对WPF组合框项目(与可观察的集合绑定)进行分类,并且每个类别必须显示不同的背景色

来自分类Dev

WPF / MVVM-子控件的绑定集合

来自分类Dev

WPF中MVVM的项目结构

来自分类Dev

在WPF MVVM中绑定图像

来自分类Dev

MVVM WPF中的数据绑定

Related 相关文章

  1. 1

    从可观察的集合中动态删除项目-WPF

  2. 2

    WPF可观察的集合,动态类型

  3. 3

    C# WPF 如何在以编程方式更新数据网格后更新可观察集合 - 没有 MVVM

  4. 4

    由c#wpf中的类类型的可观察集合填充的过滤器lisbox

  5. 5

    WPF双向数据绑定到可观察集合中的自定义数据类型

  6. 6

    WPF Listview使用ListView Item中的按钮更改可观察的集合属性

  7. 7

    由c#wpf中的类类型的可观察集合填充的过滤器lisbox

  8. 8

    嵌入式Winform图表可以绑定到WPF中的可观察集合吗?

  9. 9

    如何将ListboxItem绑定为WPF中的可观察集合,并在顶部插入新记录

  10. 10

    刷新WPF Datagrid是否不绑定到可观察的集合?

  11. 11

    在WPF MVVM中处理模型中的集合

  12. 12

    在WPF MVVM中处理模型中的集合

  13. 13

    如何在可观察的集合WPF中设置Canvas.Left和Canvas.Top-Windows Phone 8.1

  14. 14

    WPF将组合框绑定到LINQ填充的可观察集合

  15. 15

    WPF TreeView的C#可观察集合LDAP路径子级

  16. 16

    WPF将项目添加到可观察的集合并打印。返回对象参考错误

  17. 17

    如何将WPF TabControl ContentTemplate绑定到不同ViewModel的可观察集合

  18. 18

    WPF将datagrid绑定到字符串可观察的集合

  19. 19

    将可观察的集合绑定到MVVM中的包装面板

  20. 20

    在mvvm light中获取可观察的排序集合

  21. 21

    MVVM跨WPF区域实现

  22. 22

    在WPF应用程序的mvvm中实现导航的最佳方法

  23. 23

    MVVM绑定可观察的集合以查看吗?

  24. 24

    使用MVVM保存/加载可观察的集合

  25. 25

    如何对WPF组合框项目(与可观察的集合绑定)进行分类,并且每个类别必须显示不同的背景色

  26. 26

    WPF / MVVM-子控件的绑定集合

  27. 27

    WPF中MVVM的项目结构

  28. 28

    在WPF MVVM中绑定图像

  29. 29

    MVVM WPF中的数据绑定

热门标签

归档