我正在使用 Prism 6 创建我的应用程序,并且我需要根据日期为 DataGridRows 的背景着色。我开始创建一个 Blend SDK 触发器来接受两个参数:MinDate 和 MaxDate。然后该操作将设置背景颜色。但是,在使用触发器时,我遇到了障碍。它在集合中不被接受,当我使用 DataTemplate 时,我似乎无法让触发器执行。
这是触发器的代码。除了调用操作之外,它实际上并没有做任何事情,因为我想确保它在编码逻辑以检查日期之前正在执行。
public class AnniversaryTrigger: TriggerBase<DataGridRow>
{
public DateTime MaxDate
{
get { return (DateTime)GetValue(MaxDateProperty); }
set { SetValue(MaxDateProperty, value); }
}
public DateTime MinDate
{
get { return (DateTime)GetValue(MinDateProperty); }
set { SetValue(MinDateProperty, value); }
}
protected override void OnAttached()
{
AssociatedObject.Loaded += OnLoaded;
AssociatedObject.Unloaded += OnUnloaded;
}
protected override void OnDetaching()
{
AssociatedObject.Loaded -= OnLoaded;
AssociatedObject.Unloaded -= OnUnloaded;
}
private void OnLoaded(object sender, System.Windows.RoutedEventArgs e)
{
AssociatedObject.DataContextChanged += OnDataContextChanged;
Refresh();
}
private void OnUnloaded(object sender, System.Windows.RoutedEventArgs e)
{
AssociatedObject.DataContextChanged -= OnDataContextChanged;
}
private void Refresh()
{
base.InvokeActions(null);
}
private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
Refresh();
}
#region Dependency Properties
public static readonly DependencyProperty MaxDateProperty =
DependencyProperty.Register("MaxDate", typeof(DateTime), typeof(AnniversaryTrigger), new PropertyMetadata(null));
public static readonly DependencyProperty MinDateProperty =
DependencyProperty.Register("MinDate", typeof(DateTime), typeof(AnniversaryTrigger), new PropertyMetadata(null));
#endregion
}
DataTemplate 如下,并附加到 DataGrid ItemTemplate。
<UserControl.Resources>
<DataTemplate x:Key="AnniversaryTemplate">
<DataGridRow>
<i:Interaction.Triggers>
<b:AnniversaryTrigger MinDate="{Binding MinDate}"
MaxDate="{Binding MaxDate}" >
<ei:ChangePropertyAction PropertyName="Background">
<ei:ChangePropertyAction.Value>
<SolidColorBrush Color="Yellow"/>
</ei:ChangePropertyAction.Value>
</ei:ChangePropertyAction>
</b:AnniversaryTrigger>
</i:Interaction.Triggers>
</DataGridRow>
</DataTemplate>
</UserControl.Resources>
这是数据网格:
<DataGrid ItemsSource="{Binding FalseAlarmHistory}" AutoGenerateColumns="False" IsReadOnly="True" ItemTemplate="{DynamicResource AnniversaryTemplate}" >
<DataGrid.Columns>
<DataGridTextColumn Header="Incident ID"
Binding="{Binding IncidentID}" />
<DataGridTextColumn Header="Incident Date"
Binding="{Binding IncidentDate, StringFormat=d}" />
<DataGridTextColumn Header="Incident Time"
Binding="{Binding IncidentTime}" />
<DataGridTextColumn Header="Notes"
Binding="{Binding Notes}" />
</DataGrid.Columns>
</DataGrid>
任何指导表示赞赏。
事件历史视图模型:
public class FalseAlarmHistoryViewModel : ValidatingBindableBase, IConfirmNavigationRequest
{
private IFalseAlarmService _service;
private ICustomerService _custsvc;
private string _title;
private IEventAggregator _eventAggregator;
public string Title { get => _title; set => SetProperty(ref _title, value); }
public FalseAlarmHistoryViewModel(IFalseAlarmService service, ICustomerService custsvc, IEventAggregator eventAggregator)
{
_service = service;
_custsvc = custsvc;
_eventAggregator = eventAggregator;
Title = "False Alarms History";
_eventAggregator.GetEvent<PermitSelectedChangedEvent>().Subscribe(PermitIdChanged);
}
//todo Color DataGrid Rows based on the anniversary year the false alarm occurred.
// See https://stackoverflow.com/questions/14997250/datagrid-row-background-color-mvvm
// Add unmapped item to the FalseAlarmHistory entity that returns the year based on the anniversary year. 0 = current year, 1 = 1 year ago, etc.
// Translate the year number into a color that will be used on the DataGrid row. Make the color configurable (in app.config at least).
//todo Initial sort should be most recent first.
private void PermitIdChanged(int obj)
{
FalseAlarmHistory = new ListCollectionView(_service.GetFalseAlarmHistoryByPermitId(_custsvc.CurrentPermitId).ToList());
RaisePropertyChanged(nameof(FalseAlarmHistory));
}
public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
{
continuationCallback(true);
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
FalseAlarmHistory = new ListCollectionView(_service.GetFalseAlarmHistoryByPermitId(_custsvc.CurrentPermitId).ToList());
RaisePropertyChanged(nameof(FalseAlarmHistory));
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
#region Commands
#endregion
#region Event Handlers
#endregion
#region Bound Controls
public ICollectionView FalseAlarmHistory { get; private set; }
#endregion
#region Bound Commands
#endregion
}