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

时间:2016-07-12 05:46:36

标签: wpf xaml mvvm datagrid resourcedictionary

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

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

请评论您的建议。

由于 Mathee

1 个答案:

答案 0 :(得分:0)

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

如果是,则必须使用Application Settings。右键单击 Project&gt;属性&gt;设置,并创建类型为System.Windows.Style的设置(PresentationFramework.dll程序集)。

如果不是,请使用DynamicResource绑定代替StaticResource

Settings class in C# (Codeproject)

How to create Application Settings

Using Settings in C#