Binding a DataTrigger to the IsChecked property of a flag

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!

+6
source share
1 answer

Here is an example that works for me using C # classes, not a DataSet.

Xaml

 <Page.Resources> <Style x:Key="RowStyle" TargetType="{x:Type DataGridRow}"> <Style.Triggers> <DataTrigger Binding="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}" Value="True"> <Setter Property="Background" Value="Blue"/> </DataTrigger> </Style.Triggers> </Style> </Page.Resources> <Page.DataContext> <Samples:DataGridRowHighlightViewModels/> </Page.DataContext> <Grid> <DataGrid ItemsSource="{Binding Items}" RowStyle="{StaticResource RowStyle}" CanUserAddRows="False" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridCheckBoxColumn Header="Selected" Binding="{Binding IsChecked}"/> <DataGridTextColumn Header="Name" Binding="{Binding Name}"/> </DataGrid.Columns> </DataGrid> </Grid> 

FROM#

 public class DataGridRowHighlightViewModels { public DataGridRowHighlightViewModels() { Items = new List<DataGridRowHighlightViewModel> { new DataGridRowHighlightViewModel {Name = "one"}, new DataGridRowHighlightViewModel {Name = "two"}, new DataGridRowHighlightViewModel {Name = "three"}, new DataGridRowHighlightViewModel {Name = "four"}, }; } public IEnumerable<DataGridRowHighlightViewModel> Items { get; set; } } // ViewModelBase and Set() give INotifyPropertyChanged support (from MVVM Light) public class DataGridRowHighlightViewModel : ViewModelBase { private bool _isChecked; public bool IsChecked { get { return _isChecked; } set { Set(()=>IsChecked, ref _isChecked, value); } } private string _name; public string Name { get { return _name; } set { Set(()=>Name, ref _name, value); } } } 
+2
source

Source: https://habr.com/ru/post/911889/


All Articles