从视图模型 mvvm 更改 xaml 资源字典中系统变量的值

Change value of a system variable in xaml resource dictionary from view model mvvm

我有一个 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**/>

有没有一种方法可以让我从后面的代码中为这些系统变量设置值并相应地更改数据网格样式。

请评论您的建议。

谢谢 马西

这取决于您是否要保存这些新设置?

如果是,则必须使用Application Settings。右键单击 项目 > 属性 > 设置,然后创建类型为 System.Windows.Style 的设置(PresentationFramework.dll 程序集)。

如果否,则使用 DynamicResource 绑定而不是 StaticResource

Settings class in C# (Codeproject)

How to create Application Settings

Using Settings in C#