WPF按钮的重写与资源词典的应用(词典和窗口不在同一个文件夹时,资源引用的写法比较特殊)

12 篇文章 2 订阅

学习过程:系统自带的空间设计相对较丑,想增加一个圆弧角怎么办?简单方法如下:

<Border Height="30" Width="80" CornerRadius="2" BorderThickness="1" BorderBrush="Gray" Background="Transparent">
	<Button Content="按钮3" Background="Transparent" BorderThickness="0"/>
</Border>

按钮
在Button外面,套一个Border的容器,就可以给按钮增加圆弧角,如上图按钮2所示。但如果程序中所有按钮都需要做成这样的造型呢。简单粗暴的办法就是,将所有Button外面包一个Border容器。这样做很麻烦,这时考虑用资源词典。
在项目中建一个资源词典Theme.xmal,在资源词典中重写Button的style,代码如下:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="ButtonStyle" TargetType="Button">
        <Setter Property="SnapsToDevicePixels" Value="true" />
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border x:Name="border" CornerRadius="2" BorderThickness="1" BorderBrush="Gray" Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="0:0:0.5" />
                                    <VisualTransition GeneratedDuration="0" To="Pressed" />
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).Color" Storyboard.TargetName="border">
                                            <EasingColorKeyFrame KeyTime="0" Value="Gray"/>
                                        </ColorAnimationUsingKeyFrames>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).Color" Storyboard.TargetName="border">
                                            <EasingColorKeyFrame KeyTime="0" Value="DodgerBlue"/>
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <TextBlock Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

资源做好了,接来是引用和绑定资源。词典和窗口不在同一个文件夹时,资源引用的写法比较特殊
1、窗口xmal文件与Theme.xaml在同级文件夹下的写法:

<Window.Resources>
	<ResourceDictionary>
		<ResourceDictionary.MergedDictionaries>
			<!--同级文件下的引用写法-->
			<ResourceDictionary Source="Theme.xaml"/>
		</ResourceDictionary.MergedDictionaries>
	</ResourceDictionary>
</Window.Resources>
<Grid>
	<Button Style="{StaticResource ButtonStyle}" Height="30" Width="80" Content="按钮2"/>
</Grid>

2、窗口xmal文件与Theme.xaml不在同级文件夹下的写法:

<Window.Resources>
	<ResourceDictionary>
		<ResourceDictionary.MergedDictionaries>
			<!--不同级文件下的引用写法-->
			<ResourceDictionary Source="pack://application:,,,/WpfApp1<!--项目名称-->;component/Theme.xaml"/>
		</ResourceDictionary.MergedDictionaries>
	</ResourceDictionary>
</Window.Resources>
<Grid>
	<Button Style="{StaticResource ButtonStyle}" Height="30" Width="80" Content="按钮2"/>
</Grid>

那个style看上去特复杂,其实仔细看每句话,也都能看得懂。其实看不懂,并不妨碍使用。学习编程语言就是这样的,网上的或别人的写法有太多的东西看不懂,不要紧,复制过来能用就行,后面写多了,自然就会越懂越多。不能做学院派,不要钻牛角尖,更不用知道茴香豆的“茴”字的四种写法。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

C#气氛组队员

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

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

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

打赏作者

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

抵扣说明:

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

余额充值