I believe that what I'm trying to do is quite simple, so I probably just donβt see anything obvious.
In a DataGrid, I am trying to bind a CheckBox so that when it is checked, the background color of its row changes. Each row has a CheckBox. I basically implement my own select-multiple-rows function (this is a product requirement, don't ask), and I have everything else, but this is a visual indication of the selected row.
I read this question , but where I miss my answer, this is what exactly to put BooleanPropertyOnObjectBoundToRow. I also reviewed this question and tried messing around with RelativeSource, but no luck.
I create my grid in my code, but here is my current style used for rows (which has my DataTrigger):
<Style x:Key="MyRowStyle" TargetType="DataGridRow"> <Style.Triggers> <DataTrigger Binding="{Binding IsChecked}" Value="True"> <Setter Property="Background" Value="Blue"/> </DataTrigger> </Style.Triggers> </Style>
Now, in my code, I create my DataGridTemplateColumn and use Factory to create my flags, and here is my code corresponding to the binding:
Binding checkBinding = new Binding("IsChecked"); checkBinding.Mode = BindingMode.OneWayToSource; RelativeSource relativeSource = new RelativeSource(); relativeSource.AncestorType = typeof(DataGridRow); relativeSource.Mode = RelativeSourceMode.FindAncestor; checkBinding.RelativeSource = relativeSource; factory.SetBinding(CheckBox.IsCheckedProperty, checkBinding);
What might be of interest is the fact that I set the ItemSource of my DataGrid to a DataTable, but my CheckBox column does not have a corresponding column in the DataTable. I'm just adding a column of templates separately, maybe this lack of basic storage affects this?
In any case, if you need more information, please let me know. Thanks!