【问题标题】:Change value of a system variable in xaml resource dictionary from view model mvvm从视图模型 mvvm 更改 xaml 资源字典中系统变量的值
【发布时间】: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


【解决方案1】:

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

如果是,那么您必须使用Application Settings。右键单击Project > Properties > Settings,然后创建一个System.Windows.Style 类型的设置(PresentationFramework.dll 程序集)。

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

Settings class in C# (Codeproject)

How to create Application Settings

Using Settings in C#

【讨论】:

  • 我是否必须在 resourceDictionary 中将 setter 值和 Datagrid 样式设置为动态资源。这样做时我会遇到无效错误
  • 而且我还想在用户从下拉列表中选择时立即更改样式。我们可以这样使用它吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-10
  • 2018-06-30
  • 2010-11-09
  • 2017-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多