简体   繁体   English

C#在Code Behind中动态更改XAML样式

[英]C# Changing XAML style dynamically in Code Behind

I create the style in the xaml but in the code behind I recive a NULL value how exactly I supposed to do this code? 我在xaml中创建了样式但是在后面的代码中我重新获得了一个NULL值我应该怎么做这个代码? Sorry for the question im new in this and I want to learn. 对不起我的新问题,我想学习。

This is the xaml code: 这是xaml代码:

 <Style x:Key="TextBoxProperties" x:Name="TextBoxProperties" TargetType="TextBlock">
        <Setter x:Name="textFontSize" Property="FontSize" Value="24"></Setter>
        <Setter x:Name="textTypography" Property="FontFamily" Value="Resources/Fonts/Bryant-BoldAlt_Italic__Santillana.ttf#Bryant"></Setter>
    </Style>  

This is the code behind: 这是背后的代码:

 private void btnIncreaseFont_Click(object sender, RoutedEventArgs e)
    {
        if (currentFontSize < MAX_FONTSIZE)
        {
            if (btnDecreaseFont.IsEnabled == false)
                btnDecreaseFont.IsEnabled = true;
            currentFontSize += 2;
            TextBoxProperties.Setters.Add(textFontSize);
            TextBoxProperties.Setters.Add(textTypography);
            UpdateCurrentPage();
        }
        else
            btnIncreaseFont.IsEnabled = false;

    }

If you want to change Global Font Size/Type at runtime, your best bet is set it at the parent container, eg Window, and child elements will inherit the value. 如果要在运行时更改全局字体大小/类型,最好将其设置在父容器中,例如Window,子元素将继承该值。

You can bind the global font size to a value in your DataContext(VM) 您可以将全局字体大小绑定到DataContext(VM)中的值

Eg <Window FontSize="{Binding FontSize}" ...> 例如<Window FontSize="{Binding FontSize}" ...>

See: WPF - change global font size at runtime 请参阅: WPF - 在运行时更改全局字体大小

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM