如何使DataGrid MultiDataTrigger影响代码隐藏添加DataGridTemplateColumn

时间:2012-10-23 17:32:51

标签: c# wpf binding styles datagridtemplatecolumn

所以,在我的DataGrid上,我有下面的Style,设置,绑定ReadOnly和Reordering变量使用MultiDataTrigger触发,我知道它有效,因为它变灰并将值设置为只读。唯一的问题是,如果使用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;

1 个答案:

答案 0 :(得分:1)

IsReadOnly属性用于控件以允许/禁止用户更改Control的基础文本,但是默认情况下ComboBox没有要更改的基础文本。我必须使用UIElement的IsHitTestVisible属性,来自MSDN - &gt; 获取或设置一个值,该值声明此元素是否可能作为其呈现内容的某些部分的命中测试结果返回。

  <Setter Property="IsHitTestVisible" Value="False" />