WPF XAML 为项目设置全局样式


全局资源样式属性

App.xaml

<Application.Resources>
    <ResourceDictionary><br>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Dictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries><br>
        <Style x:Key="xxx" TargetType="Button">
            <Setter Property="Foreground" Value="White"></Setter>
            <Setter Property="FontSize" Value="30"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid></Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
</Style>
        <ControlTemplate x:Key="xx" TargetType="Button">
            <Grid></Grid>
        </ControlTemplate>
    </ResourceDictionary>
</Application.Resources>

说明:

1.行类属性尽量少用,只有特殊控件 需要用到行内属性,

   正确的做法是封装统一风格的所有控件。
  (例如按钮,统一高宽,字体,字体大小,然后申明到独立的资源字典中,
    在App.xaml中引用)

2.头部资源引用情况用于 不同 Window 适应不同主题或者风格的情况。

   比如为某一个窗口申明一个当前窗口单独使用的样式。

  (例如播放器的旋转控件,只有一个页面用到,只需要在Window级引用对应资源字典)

   不放在App.xaml原因是为了降低内存消耗。

3.App.xaml 里面的资源引用适用于全局资源。理论上每一个被申明的Window
   都会创建一个对应资源字典的实例。除非是每个Window都会用到的模块,
    不然建议放到对应Window级

经典实例:

ControlStyle.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:DemoForm.UI">
    <Style x:Key="BtnControl" TargetType="Button">
        <Setter Property="FontSize" Value="15"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Height" Value="40"/>
        <Setter Property="Margin" Value="2"/>
        <!--<Setter Property="Background" Value="Red"/>-->
</Style>
</ResourceDictionary>


App.xaml
<Application
    x:Class="DemoForm.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DemoForm"
    StartupUri="MainWindow.xaml">


    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/UI/ControlStyle.xaml"></ResourceDictionary>
                <!--或者这样方式DemoForm;component/UI/Dictionary1.xaml  引用以后就可以继承了-->
            </ResourceDictionary.MergedDictionaries>
            <Style BasedOn="{StaticResource  BtnControl}" TargetType="Button" >
                <Setter Property="FontSize" Value="10" />
                <Setter Property="HorizontalContentAlignment" Value="Center" />
                <Setter Property="VerticalContentAlignment" Value="Center" />
                <Setter Property="Height" Value="40" />
                <Setter Property="Margin" Value="2" />
                <Setter Property="Template">  <!--应用于全局的控件模板-->
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border BorderThickness="1" CornerRadius="10" Background="{TemplateBinding Background}">
                                <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Border>
                            <ControlTemplate.Triggers >
                                <Trigger Property="Button.IsMouseOver" Value="True">
                                    <Setter Property="Button.Background" Value="blue"/>
                                </Trigger >
                            </ControlTemplate.Triggers >
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
</Style>
            <Style TargetType="Label">
                <!--//x:Key="LblStyle"去掉就是全局引用-->
                <Setter Property="FontSize" Value="12" />
                <Setter Property="HorizontalContentAlignment" Value="Center" />
                <Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
            <Style TargetType="TextBox">
                <!--//x:Key="TxtStyle" 去掉就是全局引用-->
                <Setter Property="FontSize" Value="12" />
                <Setter Property="HorizontalContentAlignment" Value="Center" />
                <Setter Property="VerticalContentAlignment" Value="Center" />
                <Setter Property="MaxHeight" Value="50" />
                <Setter Property="MinWidth" Value="80" />
                <Setter Property="Margin" Value="2" />
</Style>
            <!--<ControlTemplate x:Key="buttonTemplate" TargetType="Button" >
                <Border BorderThickness="1" CornerRadius="10" Background="{TemplateBinding Background}">
                    <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
                </Border>
                <ControlTemplate.Triggers >
                    <Trigger Property="Button.IsMouseOver" Value="True">
                        <Setter Property="Button.Background" Value="blue"/>
                    </Trigger >
                </ControlTemplate.Triggers >
            </ControlTemplate >-->
        </ResourceDictionary>
    </Application.Resources>
</Application>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zls365365

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值