![](/img/trans.png)
[英]UWP C# - listview change “item” background color depending of value (not the entire row)
[英]uwp: how to change background color of listview item based on its value?
編輯:UWP App與WPF應用程序不是100%相同。
我有一個帶有ListView的uwp應用程序。 在ListView中,我使用帶有測試類的DataTemplate。 它顯示測試和點的名稱。
我想要完成的是一個觸發器! 檢查點是否大於ie:50然后將ListViewItem 的背景顏色更改為紅色。
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:Tests">
<Grid>
<TextBlock Text="{x:Bind Name}" />
<TextBlock Text="{x:Bind Points}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
我發現很難讓我的listview項目顯示替代顏色。 最后,我設法通過為ListView事件處理程序ContainerContentChanging指定一個方法來完成此操作。
在列表視圖中填充每個項目時,將調用分配給此事件的方法。 這提供了更改列表視圖項的前景,背景,文本等的功能
private void listViewContentChange(ListViewBase sender, ContainerContentChangingEventArgs args) {
//this method is called for each item while it gets loaded in the listview. Here we are changing background color and text color
if (args.ItemIndex == 0) {
//colour for header
args.ItemContainer.Background = (SolidColorBrush) Application.Current.Resources["grey"];
} else {
if (args.ItemIndex % 2 == 0) {
//lighter colour
args.ItemContainer.Background = (SolidColorBrush) Application.Current.Resources["lightblue"];
} else {
//Dark colour
args.ItemContainer.Background = (SolidColorBrush) Application.Current.Resources["blue"];
}
}
您可以使用類似的內容: https : //stackoverflow.com/a/27621234/3869250
在那個例子中,海報只是檢查它是偶數還是奇數,以創建交替的顏色。
if (index % 2 == 0)
{
...
}
在您的情況下,您可以執行以下操作:
if(item.Points > 50)
{
return this.RedBackgroundStyle;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.