WPF DataGridComboBoxColum - trigger on selecting value - wpf

I have a DataGrid with several fields, one of it is a DataGridComboBoxColumn. On that one I add items dynamically depending on one field by default "Add items". If I have added Item 1 and is currently selected, I'd like that when the user clicks on the dropdown and selects Item 1 again I can trigger my source and edit that item. However currently using UpdateSourceTrigger=PropertyChanged does not trigger any changes.
Looks like UpdateSourceTrigger=Explicit is what I need, but I am not sure how to implement the UpdateSource() method to do what I want.
My xaml snippet
<DataGridComboBoxColumn x:Name="ItemColumn" Header="{x:Static res:RES.HEADER}"
ToolTipService.ToolTip="{x:Static res:RES.TOOLTIP}"
SelectedValueBinding="{Binding Path=Item, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" >
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox" BasedOn="{StaticResource DataGridComboBoxElementStyle}">
<Setter Property="ItemsSource" Value="{Binding Items, UpdateSourceTrigger=PropertyChanged}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox" BasedOn="{StaticResource DataGridComboBoxEditingElementStyle}">
<Setter Property="ItemsSource" Value="{Binding Items, UpdateSourceTrigger=PropertyChanged}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
And I got string Item and a List<string> Items properties to bind the fields.
Is it possible to get an example of how to use Explicit to get a trigger every time the user clicks on the dropdown of the Item, even if already selected? or is there any other way not using Explicit? I am using MVVM as much as possible so that's also a factor to consider.

The SelectedValueBinding source property is only set if you select a new value.
If you want to do something when the the currently selected value is selected again, you could handle this in the view by for example handling the DropDownOpened and DropDownClosed events:
private void ComboBox_Loaded(object sender, RoutedEventArgs e)
{
ComboBox cmb = (ComboBox)sender;
cmb.DropDownOpened += Cmb_DropDownOpened;
cmb.DropDownClosed += Cmb_DropDownClosed;
}
string _selectedValue;
private void Cmb_DropDownOpened(object sender, EventArgs e)
{
_selectedValue = ((ComboBox)sender).SelectedItem as string;
}
private void Cmb_DropDownClosed(object sender, EventArgs e)
{
string selectedValue = ((ComboBox)sender).SelectedItem as string;
if (selectedValue == _selectedValue)
{
//same value was selected...
}
}
XAML:
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Items, UpdateSourceTrigger=PropertyChanged}" />
<EventSetter Event="Loaded" Handler="ComboBox_Loaded" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>

Related

WPF multiple-condition binding filtering

I am new to WPF and all this magical binding and datatriggers stuff, so i ask you for a little help.
I have a simple wpf app shown on picture below.
I want my datagrid contents to reflect conditions and date filter. I already figured out how to bind datagrid rows visibility depending on event codes and checkboxes (start, stop, error). But i cant't figure out how to implement date filtering. All i want is: when "Filter by date" checkbox is checked, in my datagrid only those rows remain visible, which have date in "server time" field (i guess i need to parse it somehow from datetime) equal to selected date combobox.
Can i achieve that using xaml only? Can enyone help me to do that?
Here is xaml for my datagrid:
<DataGrid
Grid.Row="1"
Margin="5"
AutoGenerateColumns="False"
IsReadOnly="True"
ItemsSource="{Binding LogEntries}"
Style="{DynamicResource Helvetica}">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding event_code}" Value="1">
<Setter Property="Background" Value="LightGreen" />
<Setter Property="Visibility" Value="{Binding IsChecked, ElementName=StartShowChecked, Converter={StaticResource BooleanToVisibilityConverter}}" />
</DataTrigger>
<DataTrigger Binding="{Binding event_code}" Value="2">
<Setter Property="Background" Value="LightGray" />
<Setter Property="Visibility" Value="{Binding IsChecked, ElementName=StopShowChecked, Converter={StaticResource BooleanToVisibilityConverter}}" />
</DataTrigger>
<DataTrigger Binding="{Binding event_code}" Value="3">
<Setter Property="Background" Value="#FFEA816F" />
<Setter Property="Visibility" Value="{Binding IsChecked, ElementName=ErrorShowChecked, Converter={StaticResource BooleanToVisibilityConverter}}" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding server_datetime, StringFormat=\{0:dd.MM.yy HH:mm:ss\}}" Header="Server time" />
<DataGridTextColumn Binding="{Binding user_datetime, StringFormat=\{0:dd.MM.yy HH:mm:ss\}}" Header="Client time" />
<DataGridTextColumn
Width="*"
Binding="{Binding log_entry}"
Header="Entry" />
</DataGrid.Columns>
Can i achieve that using xaml only?
No you can't because XAML is a markup language and nothing else.
What you shold do is to bind the SelectedItem of the date ComboBox to a DateTime property of your view model and bind the IsChecked property of the "filter by" CheckBox to a bool property of your view model and filter the LogEntries source collection when the IsChecked source property is set, e.g.:
public class ViewModel : INotifyPropertyChanged
{
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set
{
_isChecked = value;
OnPropertyChanged();
//filter collection:
LogEntries = allLogEntries.Where(x => x.ServerTime == SelectedDate).ToList();
}
}
private List<LogEntry> _logEntries;
public List<LogEntry LogEntries
{
get { return _logEntries; }
set
{
_logEntries = value;
OnPropertyChanged();
}
}
//...
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
I doubt there is a way to do it in XAML only, since filtering requires you to specify how to filter (for example by creating a predicate).
What I recommend is getting an ICollectionView from your ItemsSource (which I assume is an ObservableCollection) and set it's Filter property.
Check out this answer for more details : Filter a DataGrid in WPF

Show popup with binding on DataGrid row mouse over

When a user hovers a row in a DataGrid I want to show a popup with some information about this row.
I've stuck how to bind DataTrigger to every row in dynamically populated DataGrid table.
I've found solutions only for tooltips but tooltips don't suit me because I need to have more control on popup (don't hide it immediately when a user moved a mouse cursor to another control, ability to click in popup with mouse etc.)
Here is XAML code where I'm trying to bind popup DataTrigger to every DataGrid row (I've put comments with questions in the code below)
<Window x:Class="VKPN.UI.Windows.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:VKPN.UI.Windows"
mc:Ignorable="d"
Title="TestWindow" SizeToContent="WidthAndHeight">
<Grid>
<Popup Name="UserPopup" Placement="RelativePoint" HorizontalOffset="-5" VerticalOffset="0"
PlacementTarget="{Binding ElementName=ThisUserControl}">
<Popup.Style>
<Style TargetType="Popup">
<Style.Triggers>
<!--How to specify binding to every DataGridTable row below?-->
<DataTrigger Binding="{Binding ElementName=DataGridTable, Path=???}" Property="IsMouseOver" Value="True">
<Setter Property="IsOpen" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Popup.Style>
<Label>
<Label.Style>
<Style TargetType="Label">
<Style.Triggers>
<!--How to specify binding to every DataGridTable row below?-->
<DataTrigger Binding="{Binding ElementName=???}" Property="IsMouseOver" Value="True">
<!--DataGrid row has a column "id" which I want to show in the label. Did I do it correct below?-->
<Setter Property="Content" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=DataContext.id}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
</Popup>
<DataGrid Name="DataGridTable" ItemsSource="{Binding}" IsReadOnly="True" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto">
</DataGrid>
</Grid>
</Window>
Please help me to figure out how to do it.
Create 2 AttachedProperty called RowPopup and ShowPopup.
RowPopup will hold a reference to Popup control, and ShowPopup will show/hide this Popup based on DataGridRow.IsMouseOver property. These are extremely easy to implement.
Create a Style with TargetType DataGridRow.
Example,
xmlns:local="clr-namespace:MyNamespace"
<Style TargetType="DataGridRow" x:Key="RowStyleKey">
<Setter Property="local:CustomADP.RowPopup" Value="{Binding ElementName=Popup1}"/>
<Setter Property="local:CustomADP.ShowPopup" Value="{Binding IsMouseOver, Mode=OneWay, RelativeSource={RelativeSource Self}}"/>
</Style>
<DataGrid RowStyle="{StaticResource RowStyleKey}" ... />
AttachedProperties Code :
public class CustomADP
{
/********* Set Popup to show ****************/
public static Popup GetRowPopup(DependencyObject obj)
{
return (Popup)obj.GetValue(RowPopupProperty);
}
public static void SetRowPopup(DependencyObject obj, Popup value)
{
obj.SetValue(RowPopupProperty, value);
}
public static readonly DependencyProperty RowPopupProperty =
DependencyProperty.RegisterAttached("RowPopup", typeof(Popup), typeof(CustomADP), new PropertyMetadata(null));
/************* Show Hide using IsOpen property ************/
public static bool GetShowPopup(DependencyObject obj)
{
return (bool) obj.GetValue(ShowPopupProperty);
}
public static void SetShowPopup(DependencyObject obj, bool value)
{
obj.SetValue(ShowPopupProperty, value);
}
// Using a DependencyProperty as the backing store for ShowPopup. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ShowPopupProperty =
DependencyProperty.RegisterAttached("ShowPopup", typeof(bool), typeof(CustomADP), new PropertyMetadata(false, new PropertyChangedCallback(ShowPopupCallback)));
private static void ShowPopupCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (!(d is DataGridRow))
return;
if (((DataGridRow)d).IsFocused == true)
{
Popup p = CustomADP.GetRowPopup(d);
p.IsOpen = Convert.ToBoolean(e.NewValue);
}
else
{
Popup p = CustomADP.GetRowPopup(d);
p.IsOpen = Convert.ToBoolean(e.NewValue);
}
}
}

WPF : DataGrid Get checked columns

I've a DataGrid that is bound to a Datatable object. DataGrid generates columns automatically.
<DataGrid
Name="TimeTableDataGrid"
AutoGeneratingColumn="TimeTableDataGrid_OnAutoGeneratingColumn"
ItemsSource="{Binding TimeTable,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,IsAsync=True}"
EnableRowVirtualization="True"
EnableColumnVirtualization="True"
VirtualizingStackPanel.IsVirtualizing="True">
</DataGrid>
I've use AutoGeneratingColumn event to add checkbox in DataGrid columns .
private void TimeTableDataGrid_OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
e.Column.Width = new DataGridLength(_columnWidth);
e.Column.HeaderTemplate = (DataTemplate) Resources["HeaderTemplate"];
}
HeaderTemplate :
<DataTemplate x:Key="HeaderTemplate"
x:Name="HeaderTemplate">
<CheckBox></CheckBox>
</DataTemplate>
How can I determine that which checkboxes are selected ?
you can create the style for your header and update your checkbox like below:
<Style x:Key="HeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<CheckBox Command="{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" CommandParameter="{TemplateBinding Content}"></CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
and update your autogenererating handler as :
e.Column.Width = new DataGridLength(_columnWidth);
e.Column.Header = e.PropertyName;
e.Column.HeaderStyle = (Style)Resources["HeaderStyle"];
so here you can bind the Command for checkbox to your viewmodel command and send the unique commandparameter that can be header. In viewmodel you can have list (of string). In the command handler you can upate that list to contain which header's checkbox is checked/unchecked
Thanks

How to use IsKeyboardFocusWithin and IsSelected together?

I have a style defined for my ListBoxItems with a trigger to set a background color when IsSelected is True:
<Style x:Key="StepItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Padding="0" SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="#40a0f5ff"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
This style maintains the selected item even when the ListBox and ListBoxItem loses focus, which in my case is an absolute must.
The problem is that I also want the ListBoxItem to be selected when one of its TextBox's child gets focused. To achieve this I add a trigger that sets IsSelected to true when IsKeyboardFocusWithin is true:
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
When I add this trigger the Item is selected when the focus is on a child TextBox, but the first behaviour disappears. Now when I click outside the ListBox, the item is de-selected.
How can I keep both behaviours?
When your listbox looses focus, it will set selected item to null because of your trigger. You can select on focus using some code behind that will not unselect when you loose focus.
XAML:
<Window x:Class="SelectedTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<StackPanel>
<TextBox Text="Loose focus here" />
<ListBox Name="_listBox" ItemsSource="{Binding Path=Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" GotFocus="OnChildGotFocus">
<TextBox Text="{Binding .}" Margin="10" />
<TextBox Text="{Binding .}" Margin="10" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" SnapsToDevicePixels="true" Background="Transparent">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</StackPanel>
</Window>
Code behind:
private void OnChildGotFocus(object sender, RoutedEventArgs e)
{
_listBox.SelectedItem = (sender as StackPanel).DataContext;
}
"When I add this trigger the Item is selected when the focus is on a child TextBox, but the first behaviour disappears. Now when I click outside the ListBox, the item is de-selected."
Actually, I don't think it has lost that original behavior. What I suspect is happening is you're clicking directly in the textbox from somewhere else so the underlying ListBoxItem never actually became selected. If it did however, you'd see the selection would still remain after you left as you want.
You can test this by forcing the ListBoxItem to be selected by clicking directly on it (side-note: you should always give it a background, even if just 'transparent' so it can receive mouse clicks, which it won't if it's null) or even just hitting 'Shift-Tab' to set the focus there, back from the textbox.
However, that doesn't solve your issue, which is that the TextBox gets the focus but doesn't let the underlying ListBoxItem know about it.
The two approaches you can use for that are an event trigger or an attached behavior.
The first is an event trigger on the IsKeyboardFocusWithinChanged event where you set 'IsSelected' to true if the keyboard focus changed to true. (Note: Sheridan's answer does a faux-change-notification but it should not be used in cases where you can multi-select in the list because everything becomes selected.) But even an event trigger causes issues because you lose the multi-select behaviors such as toggling or range-clicking, etc.
The other (and my preferred approach) is to write an attached behavior which you set on the ListBoxItem, either directly, or via a style if you prefer.
Here's the attached behavior. Note: You again would need to handle the multi-select stuff if you want to implement that. Also note that although I'm attaching the behavior to a ListBoxItem, inside I cast to UIElement. This way you can also use it in ComboBoxItem, TreeViewItem, etc. Basically any ContainerItem in a Selector-based control.
public class AutoSelectWhenAnyChildGetsFocus
{
public static readonly DependencyProperty EnabledProperty = DependencyProperty.RegisterAttached(
"Enabled",
typeof(bool),
typeof(AutoSelectWhenAnyChildGetsFocus),
new UIPropertyMetadata(false, Enabled_Changed));
public static bool GetEnabled(DependencyObject obj){ return (bool)obj.GetValue(EnabledProperty); }
public static void SetEnabled(DependencyObject obj, bool value){ obj.SetValue(EnabledProperty, value); }
private static void Enabled_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var attachEvents = (bool)e.NewValue;
var targetUiElement = (UIElement)sender;
if(attachEvents)
targetUiElement.IsKeyboardFocusWithinChanged += TargetUiElement_IsKeyboardFocusWithinChanged;
else
targetUiElement.IsKeyboardFocusWithinChanged -= TargetUiElement_IsKeyboardFocusWithinChanged;
}
static void TargetUiElement_IsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var targetUiElement = (UIElement)sender;
if(targetUiElement.IsKeyboardFocusWithin)
Selector.SetIsSelected(targetUiElement, true);
}
}
...and you simply add this as a property setter in your ListBoxItem's style
<Setter Property="behaviors:AutoSelectWhenAnyChildGetsFocus.Enabled" Value="True" />
This of course assumes you've imported an XML namespace called 'behaviors' that points to the namespace where the class is contained. You can put the class itself in a shared 'Helper' library, which is what we do. That way, everywhere we want it, its a simple property set in the XAML and the behavior takes care of everything else.
I figured out that IsKeyboardFocusWithin is not the best solution.
What I did in this case was to set the style on all of the controls used as DataTemplate to send the GotFocus-event to be handled in code behind. Then, in code behind, I searched up the visual tree (using VisualTreeHelper) to find the ListViewItem and set IsSelected to true. This way it does not "touch" the DataContext and works just with the View elements.
<Style TargetType="{x:Type Control}" x:Key="GridCellControlStyle">
...
<EventSetter Event="GotFocus" Handler="SelectListViewItemOnControlGotFocus"/>
...
private void SelectListViewItemOnControlGotFocus(object sender, RoutedEventArgs e)
{
var control = (Control)sender;
FocusParentListViewItem(control);
}
private void FocusParentListViewItem(Control control)
{
var listViewItem = FindVisualParent<ListViewItem>(control);
if (listViewItem != null)
listViewItem.IsSelected = true;
}
public static T FindVisualParent<T>(UIElement element) where T : UIElement
{
UIElement parent = element;
while (parent != null)
{
var correctlyTyped = parent as T;
if (correctlyTyped != null)
{
return correctlyTyped;
}
parent = VisualTreeHelper.GetParent(parent) as UIElement;
}
return null;
}

WPF Datagrid Cell with Validation Error Style

I am trying to change the default style of a DataGridCell (within a WPF Toolkit DataGrid) when there is a validation error. The default is a red border. How can I put my own template?
Thanks.
Try this:
<!-- Cell Style -->
<Style x:Key="CellErrorStyle" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
<Setter Property="Background" Value="Yellow"/>
</Trigger>
</Style.Triggers>
</Style>
And use it:
<DataGrid.Columns>
<DataGridTextColumn
ElementStyle="{StaticResource CellErrorStyle}">
</DataGridTextColumn>
</DataGrid.Columns>
There's a nice tutorial from Diederik Krols that does exactly what you're asking for the WPF Toolkit DataGrid.
One kind of solution is below, but first, let me share my findings.
It seems like the validation errors never reach inside the column's ElementStyle or CellStyle. The reason I suspect this is because it reaches and can be used in the column's EditingElementStyle and the datagrid's RowStyle.
For example, you can set the style based on Validation.HasError:
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
or you can set the Validation.ErrorTemplate as well:
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Border BorderBrush="Red" BorderThickness="3">
<AdornedElementPlaceholder />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
and both work just fine. Same on the EditingElementStyle. Neither of these really solves the problem: changing the row style obviously doesn't show which cell the error is in, and the editing style is not visible once the text box is defocused.
Unfortunately, for some reason, the same method doesn't work on the ElementStyle or the CellStyle. I'm inclined to believe this is a bug because in this tutorial it says after showing an example of setting a Validation.HasError triggered style on the EditingElementStyle:
You can implement more extensive customization by replacing the
CellStyle used by the column.
The solution
One workaround is not to use a trigger but rather bind the background (or whatever style property you want) of the cell to a new property of the data object. I'll show what I mean.
In this example, there are products, they have a category, which will be displayed in a text column in the datagrid. Here's the XAML:
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Products}">
<DataGrid.Columns>
<!-- other columns -->
<DataGridTextColumn Header="Category">
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background"
Value="{Binding Mode=OneWay, Path=CategoryErrorBackgroundColor}" />
</Style>
</DataGridTextColumn.CellStyle>
<DataGridTextColumn.Binding>
<Binding Path="Category" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<ExceptionValidationRule />
</Binding.ValidationRules>
</Binding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
<!-- other columns -->
</DataGrid.Columns>
</DataGrid>
And here's the Product class:
public class Product : INotifyPropertyChanged
{
// ...other fields and properties
private string category;
private SolidColorBrush categoryErrorBackgroundColor;
public string Category
{
get
{
return category;
}
set
{
// validation checks
if (value.Lenght < 5)
{
CategoryErrorBackgroundColor = Brushes.Red;
// Notice that throwing is not even necessary for this solution to work
throw new ArgumentException("Category cannot be shorter than 5 characters.");
}
else
{
CategoryErrorBackgroundColor = Brushes.Transparent;
}
category = value;
}
}
// This is the property I'm binding to the cell's background
// It has to have the appropriate type
public SolidColorBrush CategoryErrorBackgroundColor
{
get
{
return categoryErrorBackgroundColor;
}
set
{
categoryErrorBackgroundColor = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
Obviously, the huge downside of this solution is that it requires one (or more if you want more complex styles) style property for every property in the data object, and it needs a lot of manual settings of these properties. But it is kind of a solution nevertheless.

Resources