前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF XAML 为项目设置全局样式

WPF XAML 为项目设置全局样式

作者头像
zls365
发布2021-10-19 09:51:51
1.6K0
发布2021-10-19 09:51:51
举报

全局资源样式属性

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>
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-03-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 CSharp编程大全 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档