Wpf 使用DataTemplate中应用的样式绑定到DataGridTemplateColumn?
我有一个组成Wpf 使用DataTemplate中应用的样式绑定到DataGridTemplateColumn?,wpf,xaml,data-binding,styles,datatemplate,Wpf,Xaml,Data Binding,Styles,Datatemplate,我有一个组成数据模板的组合框,我无法将其IsEnabled属性绑定到模板化的DataGridTemplateColumn上的IsReadOnly属性 我在VS输出窗口中收到以下错误: 在“对象”“ContentPresenter”上找不到“IsReadOnly”属性 组合框样式: <Style TargetType="{x:Type ComboBox}" x:Key="ProficiencyColumnComboBoxStyle"> <Setter Property="
数据模板的组合框
,我无法将其IsEnabled
属性绑定到模板化的DataGridTemplateColumn
上的IsReadOnly
属性
我在VS输出窗口中收到以下错误:
在“对象”“ContentPresenter”上找不到“IsReadOnly”属性
组合框样式:
<Style TargetType="{x:Type ComboBox}" x:Key="ProficiencyColumnComboBoxStyle">
<Setter Property="IsEnabled"
Value="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
Path=IsReadOnly, Converter={StaticResource BooleanOppositeConverter}}" />
</Style>
我认为问题在于如何指定用于标识我的DataGridColumn
的RelativeSource
。我试过:
RelativeSource={relativesourcetemplatedparent}
RelativeSource-AncestorType={x:Type-DataGridColumn}
RelativeSource-AncestorType={x:Type-DataGridTemplateColumn}
我尝试过向这个样式中添加其他的setter,它们确实有效,所以我知道样式和DataTemplate
正在应用于控件
p.S.
我使用了相同的技术将DataTemplate
中的另一个ComboBox
绑定到其模板列的父DataGrid
上的属性不同之处在于,我在这里使用转换器,并尝试绑定到列(而不是网格)上的属性。但是,即使我从上述样式中删除转换器,也不会发生绑定。将列创建为资源可能会解决问题,这样,您就可以使用StaticResource
来定位它。e、 g
<DataGrid.Resources>
<DataGridTemplateColumn x:Key="Column" .../>
</DataGrid.Resources>
<DataGrid.Columns>
<StaticResource ResourceKey="Column"/>
</DataGrid.Columns>
正如在注释Binding.Source
中提到的,根据结构的不同,通过列的名称也可以工作。如果可以将带有引用的零件移动到被引用元素的资源中,通常可以消除周期性依赖性错误。您只需在需要该部件的地方使用StaticResource
扩展,这并不太方便。尝试以下方法:
<Style TargetType="{x:Type ComboBox}" x:Key="ProficiencyColumnComboBoxStyle">
<Setter Property="IsEnabled"
Value="{Binding IsReadOnly, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}, Converter={StaticResource BooleanOppositeConverter}}"/>
</Style>
DataGridCell.IsReadOnly应该从其DataGridColumn.IsReadOnly中提取值。不能像这样绑定到列,它们不是逻辑树或可视树中的项目,它们只是数据,因此RelativeSource将失败。你应该,你肯定会得到一个绑定错误。@H.B.这是一篇优秀的文章,汉克斯提醒一下!在我的输出窗口中发现的绑定错误存在于问题中。。。如果您知道实现我的目标的解决方法或替代方法,请发布一个答案,我很乐意接受。这就是TemplatedParent的错误。我认为,对于绑定到列,您应该会得到一些关于找不到源的信息。无论如何,通常只能使用Binding.Source
结合使用绑定到DG列,循环依赖性可能会带来复杂性。信息性答案为+1。很抱歉,我没有接受我在评论中所说的,但是Stipo提供了一个简洁的工作答案,而这种方法需要重新构造我的XAML。将来可能会用到这个:)@robjb:我不得不说,这是一个非常有趣的想法,值得接受。
<Style TargetType="{x:Type ComboBox}" x:Key="ProficiencyColumnComboBoxStyle">
<Setter Property="IsEnabled"
Value="{Binding IsReadOnly, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}, Converter={StaticResource BooleanOppositeConverter}}"/>
</Style>