<Application xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation 
             xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml  
             x:Class="MyApp.App"
    <Application.Resources> 
        <Style x:Name="MyTextBoxStyle" TargetType="TextBlock"

        </Style> 
    </Application.Resources> 
</Application>

如何通过后台编码将这一样式应用到控件上呢?其实很简单,只需要一行代码即可:

textBlock.Style = Application.Current.Resources["MyTextBoxStyle"] as Style;

上面的样式是定义在App.xml中的全局样式,如果我们需要应用定义在页面中的样式,只需要稍作调整,代码如下:

textBlock.Style = Resources["MyTextBoxStyle"] as Style;

相关文章:

  • 2021-07-12
  • 2022-01-11
  • 2022-12-23
  • 2021-06-17
  • 2021-05-29
  • 2021-07-06
  • 2021-12-25
  • 2021-06-01
猜你喜欢
  • 2022-12-23
  • 2021-07-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-04
相关资源
相似解决方案