1

因此,在我的 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;
4

1 回答 1

1

IsReadOnly属性用于控件以允许/禁止用户更改控件的基础文本,但是默认情况下,组合框没有要更改的基础文本。我不得不使用IsHitTestVisibleUIElement 的属性,来自MSDN ->获取或设置一个值,该值声明该元素是否可以作为命中测试结果从其渲染内容的某些部分返回。

  <Setter Property="IsHitTestVisible" Value="False" />
于 2012-10-23T18:04:39.097 回答