[英]WPF ListView alter foreground using codebehind for just a cell
嗨,我有一个两列的listview
,“ Folder ”和“ Status ”。 如何将“ 状态 ”成员的前景更改为绿色(如果其数据为“ 锁定 ”),将其更改为红色(如果其数据为“ 未锁定 ”)。
例
Folder | Status
----------+--------------------------------------------
xxxx | Locked <--To be appeared as green
yyyyy | Unlocked <-- To be appeared as red
对不起,我的英语不好。
编辑: - - - - - - - - - - - - - - - - -
嘿,我尝试了您的解决方案,但仍然无法正常工作。
看看我做错了什么。
<ListView x:Name="FoldersListView" Margin="11,202,8,98" Foreground="Black" Background="#FFFFCFCF" BorderBrush="Transparent" FontWeight="Bold">
<ListView.View>
<GridView>
<GridViewColumn Header="Folder" Width="300" DisplayMemberBinding="{Binding Path = FolderPath}"/>
<GridViewColumn Header="Status" Width="100" DisplayMemberBinding="{Binding Path = FolderStatus}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Locked}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Locked}" Value="True">
<Setter Property="Foreground" Value="Green"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Locked}" Value="False">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
您可以将触发器用于特定的CellTemplate
<DataTemplate>
<TextBlock Text="{Binding Path=Status}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Locked}" Value="True">
<Setter Property="Foreground" Value="Green"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Locked}" Value="False">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<DataTemplate>
更新
我刚刚看到您使用命令式代码寻求解决方案。 因此,您可以忽略我的回答,这是一种明确的解决方案。
但是请注意,您试图实现的目标不应在后面的代码中实现,因为它与MVVM原理不一致
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.