I'm using Prism 6 to create my application, and I have a need to color the background of DataGridRows based on the date. I started creating a Blend SDK Trigger to accept two parameters: MinDate and MaxDate. The action would then set the background color. However, I am hitting a roadblock when it comes to using the trigger. It isn't accepted in the collection and I can't seem to get the trigger to execute when I use a DataTemplate.
Here's the code for the trigger. It doesn't actually do anything other than invoke the action because I want to make sure it is executing before coding the logic to check the date.
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
}
The DataTemplate is as follows and was attached to the 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>
Here's the DataGrid:
<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>
Any guidance is appreciated.
Incident history view model:
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
}