我有一个 WPF DataGrid,当我通过集合添加新项目时,我会在 MVVM 中使用 C# 选择该新项目(行)
// Adding the new item (which will create a new row)
ItemSourceCollection.Add(object);
// The itemSource would have refreshed, so now assign the SelectedItem of the grid
SelectedItem = ItemSourceCollection.Where(x => x.Id == Id).FirstOrDefault();
当行被选中时,这很有效。但是,行颜色与用户实际单击该行时的颜色不匹配。
所选行颜色的 xaml
<DataGrid.Resource>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="#FF0000"/>
</DataGrid.Resource>
那么当通过代码将新行添加到集合中时是否可以使用这种颜色?
最佳答案
问题是由于所选项目具有焦点和不具有焦点时有不同的画笔而引起的。所以你要么需要设置焦点,要么添加这个
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#FF0000"/>
到DataGrid.Resources
。如果您使用 .NET 4.5,则有专用的画笔,您需要添加
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#FF0000"/>
关于c# - WPF DataGrid,DataGrid 上选定的行颜色 MVVM 中的 SelectedItem 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24108932/