FaisalM
FaisalM

Reputation: 756

Binding to MainWindowViewModel property from app.xaml

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>

Upvotes: 0

Views: 61

Answers (1)

Insane
Insane

Reputation: 664

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 In your command execution, add the following code:

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

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

Upvotes: 2

Related Questions