![](/img/trans.png)
[英]Change background color of list view item in uwp whenever the value changes
[英]Put rectangle color for each item in list view UWP XAML
我想為每個項目放置不同顏色的矩形,但是我知道該怎么做。
我的XAML代碼
<ListView x:Name="listView1">
<ListView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="47"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Rectangle Fill="Red" Width="32" Height="32"
HorizontalAlignment="Left"/>
<TextBlock Text="{x:Bind}" Foreground="Black"
FontSize="15" Grid.Column="1"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<x:String>White</x:String>
<x:String>Red</x:String>
<x:String>Yellow</x:String>
<x:String>Blue</x:String>
<x:String>Green</x:String>
<x:String>Grey</x:String>
<x:String>Black</x:String>
</ListView>
當前,您有一個DataTemplate始終將顏色引用為Red
-不管您的綁定值如何,根據您的標簽, Rectangle
的Fill
仍為Red
。
為了解決此問題,您應將<Rectangle Fill="Red" Width="32" Height="32" HorizontalAlignment="Left"/>
替換為<Rectangle Fill="{x:Bind}" Width="32" Height="32" HorizontalAlignment="Left"/>
。
新的{x:Bind}
將您在下面設置的相關<x:String>
引入到Rectangle
的Fill
屬性中,結果意味着您將看到正確的顏色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.