So, on my DataGrid, I have the below Style, set up, Binding the ReadOnly and Reordering variables to be triggered using the MultiDataTrigger, I know it works because it greys out and sets values to read-only. The only problem is that if for the ComboBoxs created with the DataGridTemplateColumn don’t have their IsReadOnly value being set. How can I get this working? I don’t know how I would go about finding how the Template Column should be bound.
DataGrid Style (which is wrapped in the <DataGrid></DataGrid> tag)
<DataGrid.Style>
<Style TargetType="{x:Type DataGrid}">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ReadOnly}" Value="True"/>
<Condition Binding="{Binding Reordering}" Value="False"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="Foreground" Value="Gray" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ReadOnly}" Value="True"/>
<Condition Binding="{Binding Reordering}" Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="IsReadOnly" Value="True" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ReadOnly}" Value="False"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="IsReadOnly" Value="False" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Style>
Code for creating a a DataGridTemplateColumn
DataTemplate comboDisplayTemplate = new DataTemplate();
FrameworkElementFactory comboDisplayElement = new FrameworkElementFactory(typeof(ComboBox));
Binding comboDisplayBinding = new Binding(f.ColumnName) { UpdateSourceTrigger = UpdateSourceTrigger.LostFocus };
comboDisplayElement.SetBinding(ComboBox.SelectedValueProperty, comboDisplayBinding);
comboDisplayElement.SetValue(ComboBox.DisplayMemberPathProperty, "Value");
comboDisplayElement.SetValue(ComboBox.SelectedValuePathProperty, "Key");
Binding comboDisplayListBinding = new Binding(f.ColumnName + "List");
comboDisplayElement.SetValue(ComboBox.ItemsSourceProperty, comboDisplayListBinding);
comboDisplayTemplate.VisualTree = comboDisplayElement;
templateColumn.CellTemplate = comboDisplayTemplate;
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn() {
Header = f.ShortDisplay,
};
DataTemplate comboboxTemplate = new DataTemplate();
FrameworkElementFactory comboboxElement = new FrameworkElementFactory(typeof(ComboBox));
Binding comboboxBinding = new Binding(f.ColumnName) { UpdateSourceTrigger = UpdateSourceTrigger.LostFocus };
comboboxElement.SetBinding(ComboBox.SelectedValueProperty, comboboxBinding);
comboboxElement.SetValue(ComboBox.DisplayMemberPathProperty, "Value");
comboboxElement.SetValue(ComboBox.SelectedValuePathProperty, "Key");
Binding comboboxListBinding = new Binding(f.ColumnName + "List");
comboboxElement.SetValue(ComboBox.ItemsSourceProperty, comboboxListBinding);
comboboxTemplate.VisualTree = comboboxElement;
templateColumn.CellEditingTemplate = comboboxTemplate;
The
IsReadOnlyproperty is used for Controls to allow/disallow users to change the underlying text of a Control, however ComboBoxes do not have an underlying text to change by default. I had to use theIsHitTestVisibleproperty of UIElement, from MSDN -> Gets or sets a value that declares whether this element can possibly be returned as a hit test result from some portion of its rendered content.