作者:邵crnich | 来源:互联网 | 2023-05-17 18:09
当wpf中的datagrid失去焦点后,选中行的颜色会变得很淡,几乎看不见。网上有人给出了解决方案:
"grid1" HorizOntalAlignment="Left" 120" Margin="75,86,0,0" VerticalAlignment="Top" 282">
"编号" Binding="{Binding compan.ID}">
"公司" Binding="{Binding CompanyName}">
"固定资产" Binding="{Binding FixedAssets}" Width ="*">
"{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Yellow"/>
来源: http://www.cnblogs.com/TaiYangXiManYouZhe/p/4495986.html
但我的grid是动态创建的,如何在代码里添加呢?写法如下:
注意Resources用于接收本地属性(即XML中的整个Resources段),缺省是空的。
m_gridCtrl.Resources.Add(SystemColors.InactiveSelectionHighlightBrushKey, new SolidColorBrush(Color.FromArgb(0xff, 0x33, 0x99, 0xff)));
m_gridCtrl.Resources.Add(SystemColors.InactiveSelectionHighlightTextBrushKey, new SolidColorBrush(System.Windows.Media.Colors.White));
这么写后还有问题,执行效果如下,可以发现grid的底部也变色了。
这不是我们需要的。因为需要只对grid行做处理,XML写法如下:
<DataGrid.Resources>
<Style TargetType="DataGridRow"> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="DarkGray"/> Style.Resources> Style>
DataGrid.Resources>
来源: http://www.itkeyword.com/doc/1744825483479414248/datagrids-selected-row-color-when-inactive
m_gridCtrl.RowStyle = new Style();
m_gridCtrl.RowStyle.Resources.Add(SystemColors.InactiveSelectionHighlightBrushKey, new SolidColorBrush(Color.FromArgb(0xff,0x33,0x99,0xff)));
m_gridCtrl.RowStyle.Resources.Add(SystemColors.InactiveSelectionHighlightTextBrushKey, new SolidColorBrush(System.Windows.Media.Colors.White));