WPF-资源(样式)-《二》

1.建立资源字典

2.快速建立样式,点击编辑副本

这里可以使用Blend设计,创建资源字典,资源自动就到了文件夹中,下图是blend出来的,vs2019不会出来。

3.把2生成的样式,移动到Dictionary1.xaml

4.然后在App.xaml中引用资源

<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!--带文件夹-->
                <ResourceDictionary Source="pack://application:,,,/WpfApp1;component/Resource/Dictionary1.xaml"></ResourceDictionary>
                <!--不带文件夹-->
                <ResourceDictionary Source="pack://application:,,,/WpfApp1;component/Dictionary1.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

</Application>

5.可以直接在界面调用 

        <Button Content="Button" HorizontalAlignment="Left" Margin="176,258,0,0" VerticalAlignment="Top" Width="75" Style="{ StaticResource ButtonStyle1}"/>

6.删除key,则是整个项目都应用 

7.Dictionary1中不删除key,在界面上增加Window.Resources,并且不加key的话,就是这个界面所有按钮应用。

    <Window.Resources>
        <Style TargetType="{x:Type Button}" BasedOn="{StaticResource ButtonStyle1}"/>
    </Window.Resources>

 以此类推,其他控件的样式也是这样增加,可以形成统一管理,统一变换。

拓展:样式和模板的区别

样式:

1.样式设置,用来为一组相同控件设置统一的样式

2.只能改变控件的已有属性值(比如颜色字体)

3.样式里面可以包含控件模板

控件模板:

1.用来为一组相同控件设置统一的外形

2.只能在Border里面增加元素。可以增加任何控件

总的来说,当控件的外观不满足要求了,这个时候就使用控件模板,因为样式只是修改具体的属性值,例如颜色,宽度等等。控件模板,可以改变控件的内部结构,进行嵌套其他的控件,因此,我们一般都是使用blend选择编辑模板,生成的就是资源字典,里面包含了控件的样式和控件模板,还可以进行事件的触发修改等等。或者再进行细化,使用样式调用控件模板,把样式和控件模板分开写,这样可以达到复用的效果。

blend生成的资源字典:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="ButtonStyle2" TargetType="{x:Type Button}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
        <Setter Property="Background" Value="Blue"/>
        <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                        <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsDefaulted" Value="true">
                            <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>
                            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>
                            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/>
                            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/>
                            <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

来源:WPF-资源(样式)-《二》_wpf绑定border资源_故里2130的博客-CSDN博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

故里2130

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

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

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

打赏作者

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

抵扣说明:

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

余额充值