根据选择和值更改 WPF DataGrid 单元格的背景颜色
我正在尝试实现这一目标:当用户在 DataGrid 中选择一个或多个单元格时,所有重复项的背景颜色都应更改。
我后面有这个 xaml
<Window x:Class="NotesOnFretboard.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="true" Margin="12,110,12,29" Name="dataGrid1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" IsReadOnly="True" />
</Grid>
</Window>
代码:
public MainWindow()
{
InitializeComponent();
DataTable dt = CreateDataTable();
dataGrid1.ItemsSource = dt.DefaultView;
}
所以我使用 DataTable(10 行,25 列)填充数据网格。 在此数据表中存在许多重复值。
当用户在 DataGrid 中选择一个或多个单元格时,所有重复项的背景颜色都应更改!
请帮忙!
// 安德斯
I'm trying to achieve this: When the user selects one or multiple cells in a DataGrid all duplicates should have their background color changed.
I have this xaml
<Window x:Class="NotesOnFretboard.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="true" Margin="12,110,12,29" Name="dataGrid1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" IsReadOnly="True" />
</Grid>
</Window>
code behind:
public MainWindow()
{
InitializeComponent();
DataTable dt = CreateDataTable();
dataGrid1.ItemsSource = dt.DefaultView;
}
So I populate the datagrid using a DataTable(10 rows, 25 columns).
In this datatable there are a number of duplicate values.
When the user selects one or multiple cells in a DataGrid all duplicates should have their background color changed!
Please Help!
// Anders
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你应该使用触发器来实现这一点
查看此综合指南:设计 Microsoft 的 WPF 数据网格
you should be using triggers to achieve that
check out this comprehensive guide: Styling Microsoft’s WPF datagrid
您可以更改集合类,使其具有一个属性来指示是否应突出显示它,然后将该属性(通过转换器)绑定到要更改颜色的元素属性。
您可以响应选择/单击,并将 ItemSource 'selected' 属性更改为 true/false,具体取决于您想要的标准。
所以像这样:
You could change your collection class so that it has a property to indicate whether it should be highlighted or not, then bind that property (through a converter) to the element property you want to change colour.
You'd could respond to a selection/click and change your ItemSource 'selected' property to true/false depending on whatever criteria you desire.
So something like: