我想在后面的代码中更改WPF DataGrid的画笔颜色。 这是有效的XAML代码:
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="{x:Static SystemColors.HighlightColor}"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="{x:Static SystemColors.HighlightTextColor}"/>
</DataGrid.Resources>
是否可以在代码隐藏中设置此XAML代码(在从默认DataGrid继承的自定义类的构造函数中)?
答案 0 :(得分:1)
你可以这样做(如果你使用的是自定义类):
public class CustomDataGrid : DataGrid
{
public CustomDataGrid()
{
var converter = new BrushConverter();
var background = FindResource(SystemColors.HighlightBrushKey);
var foreground = FindResource(SystemColors.HighlightTextBrushKey);
this.Resources.Add(SystemColors.InactiveSelectionHighlightBrushKey, (Brush)converter.ConvertFromString(background.ToString()));
this.Resources.Add(SystemColors.InactiveSelectionHighlightTextBrushKey, (Brush)converter.ConvertFromString(foreground.ToString()));
}
}
答案 1 :(得分:1)
非常感谢你。你的答案是我的问题的解决方案,但提供例外。这段代码解决了问题:
var converter = new BrushConverter();
var background = FindResource(SystemColors.HighlightBrushKey);
var foreground = FindResource(SystemColors.HighlightTextBrushKey);
this.Resources.Add(SystemColors.InactiveSelectionHighlightBrushKey, (Brush)converter.ConvertFromString(background.ToString()));
this.Resources.Add(SystemColors.InactiveSelectionHighlightTextBrushKey, (Brush)converter.ConvertFromString(foreground.ToString()));