[英]Trigger Style on One Column of GridView
My question is similar to WPF Grid - How to apply a style for just one column? 我的问题类似于WPF网格-如何仅对一列应用样式? but applies to a GridView
inside of a ListView
但适用于ListView
内部的GridView
I'm trying to apply a trigger that will change the color of text within a TextBlock
only in a given column within the GridView
. 我正在尝试应用一个触发器,该触发器将仅在GridView
的给定列中更改TextBlock
本的颜色。 If I take out the line commented THIS
each textblock in the row is affected. 如果我拿出行注释掉THIS
行中的每个文字块受到影响。
<ListView>
<ListView.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="White" />
</Style>
</ListView.Resources>
<ListView.View>
<GridView x:Name="MyGridView" ColumnHeaderContainerStyle="{StaticResource GridViewColumnHeaderStyleNew}">
<GridViewColumn Width="90" Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Width="90" Header="Option" DisplayMemberBinding="{Binding Option}"/>
<GridViewColumn Width="90" Header="Dir" DisplayMemberBinding="{Binding Directory}"/>
<GridViewColumn Width="90" Header="Size" DisplayMemberBinding="{Binding Size}"/>
<GridViewColumn Width="90" Header="Status" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Status}">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="White" />
<Style.Triggers>
<DataTrigger Binding="{Binding Status}" Value="Overheating">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Foreground.Color" From="White" To="Red" Duration="0:0:2" AutoReverse="True" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
What changes to that line would make it so that only the 5 th column which contains the text block is affected by the multitrigger? 对该行进行什么更改,以使只有包含文本块的第 5列会受到多重触发的影响?
[EDIT 1] Added code for GridView
[编辑1]为GridView
添加了代码
[EDIT 2] I modified the last GridViewColumn
to give it its own style [编辑2]我修改了最后一个GridViewColumn
以使其具有自己的样式
[EDIT 3] Removed the DisplayMemberBinding
and added a Setter
property... it works :). [编辑3]删除了DisplayMemberBinding
并添加了一个Setter
属性...,它可以工作:)。
Updated source code to reflect correct answer. 更新了源代码以反映正确答案。
The easiest thing to do would be moving the style to the column's template: 最简单的方法是将样式移动到列的模板:
<GridViewColumn Width="90" Header="Status">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Status}">
<TextBlock.Style>
<!-- Style here without additional condition -->
You will need to use multi-binding and pass GridViewColumn & GridView into the converter, which decides COLUMN number. 您将需要使用多重绑定,并将GridViewColumn和GridView传递到转换器中,该转换器确定COLUMN号。 You can pass those using RelativeSource binding which will work automatically. 您可以使用将自动运行的RelativeSource绑定传递那些。 There is no "Column" property as far I can tell. 据我所知,没有“列”属性。
In conveter you do GridView.Columns.IndexOf(YourColumn). 在转换中,您执行GridView.Columns.IndexOf(YourColumn)。 Alternatively you just change your viewmodel and add new property, "StyleColumn", and use bindable columns WPF MVVM: how to bind GridViewColumn to ViewModel-Collection? 另外,您只需更改视图模型并添加新属性“ StyleColumn”,然后使用可绑定列WPF MVVM:如何将GridViewColumn绑定到ViewModel-Collection?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.