改变ListView项的前景色仅在一列 [英] Change the foreground color of a ListView item only in one column
问题描述
我有对象的名单,并在那里我显示此列表一个ListView。这样的对象具有一定的属性,它们被绑定到ListView的列。
I have a List of objects and a ListView where I display this list. Such an object has some properties, they are bound to the columns of the ListView.
<ListView x:Name="_fileNameList" FontSize="12" SourceUpdated="_fileNameList_SourceUpdated" TargetUpdated="_fileNameList_TargetUpdated">
<ListView.View>
<GridView x:Name="FileNameAttributes" >
<GridViewColumn Header="File Name" Width="200" DisplayMemberBinding="{Binding fileName}"/>
<GridViewColumn Header="Size" Width="80" DisplayMemberBinding="{Binding size}"/>
<GridViewColumn Header="Date" Width="80" DisplayMemberBinding="{Binding date}"/>
<GridViewColumn Header="Time" Width="80" DisplayMemberBinding="{Binding time}"/>
<GridViewColumn Header="New Name" Width="300" DisplayMemberBinding="{Binding newFileName}"/>
</GridView>
</ListView.View>
</ListView>
这部分工作正常。
现在我想改变 newFileName
列的前景色在单行中,但只有当它等于文件名在同一行
Now I want to change the Foreground color of the newFileName
column in a single row, but only if it is equal to the 'fileName' in the same row.
我可以做到这一点在XAML还是我得去隐藏文件code?
Can I do this in XAML or do I have to go to the code behind file?
我想最好的喜欢它,如果我能处理它在XAML,因为我觉得它是一个纯粹的设计问题,但我不知道,从哪里开始或在哪里把这个,(我可以做字符串比较在XAML?)
I would like it best if I could handle it in XAML, because I think its a pure design issue, but I have no idea, where to start or where to put this, (can I do String comparisons in XAML?)
于是,我就在文件后面code来处理这个问题。我认为有必须是提出当ListView控件已经改变了,我试过 SourceUpdated
事件的事件,但是当我改变我的列表的内容是没有进入。< BR>
接下来的问题是,如何访问这些项目的ListView ...
So I tried to handle this in the code behind file. I thought there must be an event that is raised when the ListView has changed, I tried the SourceUpdated
event, but it is not entered when I change the content of my list.
The next problem would be, how to access those ListView items ...
谁能给我一个想法,我怎么能解决这个问题?
Can anyone give me an idea how I can solve this?
推荐答案
您可以做到这一点使用MultiBinding和多转换。
您将需要编写一个多转换这需要文件名和newFileName,如果它们相等,则返回true
You can do this using MultiBinding and MultiConverter. You will need to write a MultiConverter which takes fileName and newFileName and returns true if they are equal
<ListView x:Name="_fileNameList" FontSize="12" SourceUpdated="_fileNameList_SourceUpdated" TargetUpdated="_fileNameList_TargetUpdated">
<ListView.View>
<GridView x:Name="FileNameAttributes" >
<GridViewColumn Header="File Name" Width="200" DisplayMemberBinding="{Binding fileName}"/>
<GridViewColumn Header="Size" Width="80" DisplayMemberBinding="{Binding size}"/>
<GridViewColumn Header="Date" Width="80" DisplayMemberBinding="{Binding date}"/>
<GridViewColumn Header="Time" Width="80" DisplayMemberBinding="{Binding time}"/>
<GridViewColumn Header="New Name" Width="300">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding newFileName}">
<TextBlock.Style>
<Style>
<Setter Property="TextBlock.Foreground" Value="Black"></Setter>
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource EqualityConverter}">
<Binding Path="newFileName"></Binding>
<Binding Path="fileName"></Binding>
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="TextBlock.Foreground" Value="Red"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
在code为EqualityConverter是如下:
The code for EqualityConverter is as below :
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[0].ToString().Equals(values[1].ToString()))
return true;
return false;
}
这篇关于改变ListView项的前景色仅在一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!