以编程方式更改单个 ListViewItem 布局
我有一个 ListView
,其中有一个 GridView
:
<ListView x:Name="listViewPersons" ItemsSource="{Binding Path=Persons, Mode=OneWay}" IsSynchronizedWithCurrentItem="True"
Height="Auto" HorizontalAlignment="Stretch" Margin="4,4,4,4" Grid.Row="4" VerticalAlignment="Stretch" Width="Auto">
<ListView.View>
<GridView x:Name="gridViewPersons">
<GridViewColumn Header="Enabled">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<CheckBox HorizontalAlignment="Center" IsChecked="true" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}" Width="142" />
<GridViewColumn Header="Age" DisplayMemberBinding="{Binding Path=Age}" Width="142" />
<GridViewColumn Header="Gender" DisplayMemberBinding="{Binding Path=Gender}" Width="142" />
</GridView>
</ListView.View>
</ListView>
我希望能够以编程方式更改每个 ListViewItem
(或grid)背景或前景为我想要的任何颜色,例如
listViewPersons.Items[0].Background = Brush.Red;
listViewPersons.Items[1].Background = Brush.Blue;
listViewPersons.Items[2].Background = Brush.Green
我知道前面的代码行不起作用,但它几乎解释了我想要实现的目标。 有什么帮助吗?
谢谢!
I have a ListView
with a GridView
in it:
<ListView x:Name="listViewPersons" ItemsSource="{Binding Path=Persons, Mode=OneWay}" IsSynchronizedWithCurrentItem="True"
Height="Auto" HorizontalAlignment="Stretch" Margin="4,4,4,4" Grid.Row="4" VerticalAlignment="Stretch" Width="Auto">
<ListView.View>
<GridView x:Name="gridViewPersons">
<GridViewColumn Header="Enabled">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<CheckBox HorizontalAlignment="Center" IsChecked="true" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}" Width="142" />
<GridViewColumn Header="Age" DisplayMemberBinding="{Binding Path=Age}" Width="142" />
<GridViewColumn Header="Gender" DisplayMemberBinding="{Binding Path=Gender}" Width="142" />
</GridView>
</ListView.View>
</ListView>
I want to be able to programmatically change each of the ListViewItem
s (or the rows in the grid) backgrounds or foregrounds to any color I want, for instance
listViewPersons.Items[0].Background = Brush.Red;
listViewPersons.Items[1].Background = Brush.Blue;
listViewPersons.Items[2].Background = Brush.Green
I know the previous lines of code don't work, but it pretty much explains what I want to achieve. Any help?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种方法怎么样:
确保此人员实现 INotifyPropertyChanged,并且此属性的设置器引发 ProperyChanged 事件。 该属性将如下所示:
创建一个 IValueConverter 接受 Status 类型并返回 Brushes.Black、Brushes.Red 等。如下所示:
How about this approach:
Ensure that this Persons implements INotifyPropertyChanged, and the setter for this property raises a ProperyChanged event. The property will look like this:
Create an IValueConverter that takes the type of Status and returns a Brushes.Black, Brushes.Red, etc. Something like this: