如何将样式绑定到具有样式元素的任何数据上下文的属性?
我在样式触发器内有一个数据触发器,如下所示,
<Style TargetType="DataGridCell" x:Key="ChangeAnimation" >
<Style.Triggers>
<DataTrigger Binding="{Binding Path=APropertyOfTheDataGridCellContext Bind DataContext}" Value="True" >
...
</DataTrigger>
</Style.Triggers>
</Style>
我将使用属性 CellStyle 将此样式应用于某些 DataGridColumn。我想要的是将样式的 DataTrigger 绑定到单元格的数据上下文的属性,这样我将能够在所有必需的列上使用此样式,而不必为每列创建新的样式。这可能吗?
提前致谢。
编辑:为了澄清,DataGrid 的 DataContext 是一个 ViewModel,具有名为 Rows 的属性,并且 Rows 有一个名为 Cells 的属性和一个名为 Blink 的属性。我想像这样引用样式内的属性:
<DataGrid.Resources>
<Style TargetType="DataGridCell" x:Key="ChangeAnimation" >
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Blink}" Value="True" >
...
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
这给了我一个错误,aws DataGridResources 数据上下文是包含行而不是单元格的数据上下文。如何从样式引用 DataGridCell DataContext?
I have a datatrigger inside a style trigger like the following one
<Style TargetType="DataGridCell" x:Key="ChangeAnimation" >
<Style.Triggers>
<DataTrigger Binding="{Binding Path=APropertyOfTheDataGridCellContext Bind DataContext}" Value="True" >
...
</DataTrigger>
</Style.Triggers>
</Style>
I will apply this style to some DataGridColumns using the property CellStyle. What I would like is to bind the DataTrigger of the style to a property of the datacontext of the cell, this way I will be able to use this Style on all of the required columns instead of having to create a new Style for each column. Is this possible?
Thanks in advance.
EDIT: To clarify, the DataContext of the DataGrid is a ViewModel with a property called Rows and the Rows has a property called Cells with a property called Blink. I want to reference the property inside the style like this:
<DataGrid.Resources>
<Style TargetType="DataGridCell" x:Key="ChangeAnimation" >
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Blink}" Value="True" >
...
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
This gives me an error aws the DataGridResources datacontext is the one containing the Rows not the cells one. How can I reference the DataGridCell DataContext from the style?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除
x:Key
属性,然后该属性将应用于所有列。如果您有 2 个 DataGrid,那么您应该将此样式保留在要修改的 dataGrid 的
Resources
标记中...如果relativesource 不起作用,我更愿意通过触发器更改列的模板实现类似的行为
为列编写触发器
这里
MyProperty
是DataGridRow
的DataContext
的属性,因此您可以选择任何级别的嵌套属性哟比较该特定行...唯一的缺点是您必须对需要此操作的所有列执行此操作我希望它有所帮助
Removing The
x:Key
attribute then that will be appliced to all the columns.If you have 2 DataGrids then you should keep this style in the
Resources
tag of the dataGrid you want to modify ...If RelativeSource is not working i would prefer to change the Template of the Column by a trigger to achive a similar behavior
Writing Trigger for the Column
Here
MyProperty
is the Property of theDataContext
of theDataGridRow
so you can chose what ever level of nested property yo Compare for that specific row... only downside is that you have to do this for all the column that require thisI hope it helps