因此,在我的 DataGrid 上,我有以下样式,设置、绑定要使用 MultiDataTrigger 触发的 ReadOnly 和 Reordering 变量,我知道它可以工作,因为它会变灰并将值设置为只读。唯一的问题是,如果使用 DataGridTemplateColumn 创建的 ComboBox 没有设置其 IsReadOnly 值。我怎样才能得到这个工作?我不知道我将如何去寻找应该如何绑定模板列。
DataGrid 样式(包含在<DataGrid></DataGrid>
标签中)
<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>
用于创建 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;