【发布时间】:2016-07-12 05:46:35
【问题描述】:
我有一个 Datagrid,它使用 ResourceDictionary 来定义它的样式。
<DataGrid Style="{StaticResource HistoryViewDataGridStyle}" .....>
</DataGrid>
在 ResourceDictionary 中我定义了以下样式。
<Style TargetType="{x:Type DataGrid}" x:Key="HistoryViewDataGridStyle">
<Style.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="FontSize" Value="11"/>
<Setter Property="Height" Value="30"/>
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="BorderThickness" Value="0"/>.....
现在我想为 DataGrids 使用应用程序范围的字体颜色和字体大小,用户可以在其中更改值。 我正在使用 mvvm 模型,并设法为用户提供颜色和字体大小的下拉列表。
我正在寻找一种在资源字典中使用系统变量的方法,
<sys:Double x:Key="DFontSize">12</sys:Double>
<SolidColorBrush x:Key="FontColorBrush" Color="Black"/>
<Style TargetType="{x:Type DataGrid}" x:Key="HistoryViewDataGridStyle">
<Style.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value=**FontColorBrush** />
<Setter Property="FontSize" Value=**DFontSize**/>
有没有一种方法可以让我从后面的代码中为这些系统变量设置值并相应地更改数据网格样式。
请评论您的建议。
谢谢 马西
【问题讨论】:
-
您可以使用动态资源执行此操作。 This answer 可以帮到你。
-
我是否必须在 resourceDictionary 和 Datagrid 样式中都设置 setter 值作为动态资源。这样做时我会遇到无效错误
标签: wpf xaml mvvm datagrid resourcedictionary