gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:43:43 25 4
gpt4 key购买 nike

我有一个模型:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com