从app.xaml绑定到MainWindowViewModel属性 [英] Binding to MainWindowViewModel property from app.xaml

查看:216
本文介绍了从app.xaml绑定到MainWindowViewModel属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试全局更改字体大小。为此,我在app.xaml中添加了样式。在这里,我的 FontSz 属性在MainWindowViewModel中。有什么方法可以使这种绑定成为可能吗?

I am trying to change the font-size globally. For this, I added styles in app.xaml. Here my FontSz property is in MainWindowViewModel. Is there any way to make this binding possible?

<Application.Resources>
    <Style TargetType="{x:Type Control}" x:Key="baseStyle">
        <Setter Property="FontSize" Value="{Binding Path=???.FontSz}" />
    </Style>
    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource baseStyle}"/>
    <Style TargetType="{x:Type Label}" BasedOn="{StaticResource baseStyle}"/>
</Application.Resources>


推荐答案

您将需要使用 DynamicResouce 为此。添加如下所示的系统命名空间

You will need to use DynamicResouce for this. Add the system namespace as below

xmlns:system="clr-namespace:System;assembly=mscorlib"

<Application.Resources>

    <system:Double x:Key="FontSz">20</system:Double>

    <Style x:Key="baseStyle" 
           TargetType="{x:Type Control}">
        <Setter Property="FontSize" 
                Value="{DynamicResource FontSz}"/>
    </Style>

    <Style TargetType="{x:Type Button}"
           BasedOn="{StaticResource baseStyle}"/>

    <Style TargetType="{x:Type Label}" 
           BasedOn="{StaticResource baseStyle}"/>

</Application.Resources>

MainWindowViewModel
在命令执行中,添加以下代码:

MainWindowViewModel In your command execution, add the following code:

Application.Current.Resources["FontSz"] = 18d;

您可以将字体大小从18d更改为用户在MainViewModel中选择的字体大小。

You can change the fontsize from 18d to the fontsize selected by the user in your MainViewModel.

这篇关于从app.xaml绑定到MainWindowViewModel属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆