编辑:UWP 应用程序与 WPF 应用程序并非 100% 相同。
我有一个带有 ListView 的 uwp 应用程序。在 ListView 中,我使用带有测试类的 DataTemplate。它显示测试的名称和点。
我想要完成的是一个触发器!?检查如果 Points 大于即: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 事件处理程序“容器内容更改”分配一个方法来做到这一点。
当列表视图中填充每个项目时,分配给此事件的方法将被调用。这提供了更改列表视图项的前景、背景、文本等的功能
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"];
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)