我如何根据 wpf 中的列表视图项 HarfNotu 值更改 ListView 项背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16126393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How i change ListView Item Background Color according to listview item HarfNotu value in wpf
提问by Zafer Ayan
My ListView looks like: http://oi36.tinypic.com/ek5n3o.jpg
我的 ListView 看起来像:http: //oi36.tinypic.com/ek5n3o.jpg
My listview xaml:
我的列表视图 xaml:
<ListView Name="notListView" Width="550" HorizontalAlignment="Left">
<ListView.View>
<GridView AllowsColumnReorder="true">
<GridViewColumn Header="Ders Kodu" Width="100" DisplayMemberBinding="{Binding Path=DersKodu}" />
<GridViewColumn Header="Ders Ad?" Width="200" DisplayMemberBinding="{Binding Path=DersAdi}" />
<GridViewColumn Header="Vize" Width="50" DisplayMemberBinding="{Binding Path=Vize}" />
<GridViewColumn Header="Final" Width="50" DisplayMemberBinding="{Binding Path=Final}" />
<GridViewColumn Header="Ortalama" Width="60" DisplayMemberBinding="{Binding Path=Ortalama}" />
<GridViewColumn Header="Harf Notu" Width="60" DisplayMemberBinding="{Binding Path=Harf}" />
</GridView>
</ListView.View>
</ListView>
My .cs code:
我的 .cs 代码:
notListView.ItemsSource = notGoruntule(1, 1); // notGoruntule() function returns an Arraylist, which contains my "Notlar" objects.
I tried this:
我试过这个:
ListViewItem lvitem = (ListViewItem)notListView.Items[0];
lvitem.Background = Brushes.Red;
But first line throws:
但第一行抛出:
Unable to cast object of type 'OBS_Interface_5.Classes.Notlar' to type 'System.Windows.Controls.ListViewItem'.
How i solve this problem?
我如何解决这个问题?
回答by Viv
You can Style
the ListViewItem
in xaml directly,
您可以Style
将ListViewItem
在XAML直接,
Example:
例子:
Assuming your "Harf" variable is a string, you can try
假设您的“Harf”变量是一个字符串,您可以尝试
<ListView Name="notListView"
Width="550"
HorizontalAlignment="Left">
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding Harf}"
Value="1">
<Setter Property="Background"
Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.Resources>
...
Now any ListView
Row with "Harf" Value of 1 will have a "Red" Background
现在ListView
,“Harf”值为 1 的任何行都将具有“红色”Background