[英]Can DataGrid.Resources be set in code behind?
我想在后面的代码中更改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>
是否可以在codebehind中设置此XAML代码(在从默认DataGrid继承的自定义类的构造函数中)?
您可以执行此操作(如果您使用的是自定义类):
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()));
}
}
Daniele Sartori:
非常感谢。 您的答案是针对我的问题的解决方案,但有例外。 此代码解决的问题:
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()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.