wpf关于Resource,Style的定义与引用,滑动按钮

当我们使用wpf框架去搭建自己的程序时,一般都会重写wpf原生的一些样式,以达到软件风格的统一于美化,以下介绍一下常见的几种添加Style的方式

我们以一个滑动按钮为例

1.对当前控件的样式进行更改

      <ToggleButton>
          <ToggleButton.Style>
              <Style TargetType="{x:Type ToggleButton}">
                  <Setter Property="Width" Value="45"></Setter>
                  <Setter Property="Height" Value="20"></Setter>
                  <Setter Property="Template">
                      <Setter.Value>
                          <ControlTemplate TargetType="ToggleButton">
                              <Border x:Name="border" BorderThickness="0" CornerRadius="9" BorderBrush="#aaa" Background="#2790ff">
                                  <Grid x:Name="togglebutton" HorizontalAlignment="Right" >
                                      <Border Width="17" Height="17" CornerRadius="10" Background="White"/>
                                  </Grid>
                              </Border>
                              <ControlTemplate.Triggers>
                                  <Trigger Property="IsChecked" Value="false">
                                      <Setter TargetName="border" Property="Background" Value="#ccc"/>
                                      <Setter TargetName="togglebutton" Property="HorizontalAlignment" Value="Left"/>
                                  </Trigger>
                              </ControlTemplate.Triggers>
                          </ControlTemplate>
                      </Setter.Value>
                  </Setter>
              </Style>
          </ToggleButton.Style>
      </ToggleButton>

这样就可以看到原生的样式已经被我们给替换掉了,但是当我们一个界面有多个滑动块时,总不能每一次都这样copy一遍,会出现大量的代码冗余

这时我们可以把ToggleButton的样式定义到你所需要的作用域中,并且可以给这个Resources一个

Key,(如果我们不添加 Key的话则默认在当前作用域下更改所有ToggleButton类型控件的样式)

2.添加控件资源

 <Window.Resources>
     <!--#region 开关按钮 -->
     <Style TargetType="{x:Type ToggleButton}" x:Key="toggleButtonSty">
         <Setter Property="Width" Value="45"></Setter>
         <Setter Property="Height" Value="20"></Setter>
         <Setter Property="Template">
             <Setter.Value>
                 <ControlTemplate TargetType="ToggleButton">
                     <Border x:Name="border" BorderThickness="0" CornerRadius="9" BorderBrush="#aaa" Background="#2790ff">
                         <Grid x:Name="togglebutton" HorizontalAlignment="Right" >
                             <Border Width="17" Height="17" CornerRadius="10" Background="White"/>
                         </Grid>
                     </Border>
                     <ControlTemplate.Triggers>
                         <Trigger Property="IsChecked" Value="false">
                             <Setter TargetName="border" Property="Background" Value="#ccc"/>
                             <Setter TargetName="togglebutton" Property="HorizontalAlignment" Value="Left"/>
                         </Trigger>
                     </ControlTemplate.Triggers>
                 </ControlTemplate>
             </Setter.Value>
         </Setter>
     </Style>
     <!--#endregion-->
 </Window.Resources>

我们想要改变哪个ToggleButton的样式就将哪个ToggleButton的Style绑定为这个资源,不设置则默认为原生样式

   <ToggleButton Style="{DynamicResource toggleButtonSty}"/>

3.添加资源字典 

当我们的做的项目更加复杂时则会发现当前ToggleButton样式的复用性是很高的,那么这个时候我们可以将这个资源添加到资源字典中

我们新建一个Dictionary文件,将Style放入到资源字典中,这个时候我们就会发现出现了问题,怎么样才能够使程序找得到对应的资源字典呢,以下我们介绍2种方式:首先在运行程序中找到App.xaml这个文件

1.在App中引用项目内的资源字典,这个时候,所有在程序中执行的文件都可以找得到这个字典中的资源(包括引用的其他dll)

<Application x:Class="WpfApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp"
            >
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Style.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Source中填写相对于软件运行目录下资源文件所在的位置

2.在App中引用其他项目的资源,styles为需要引用dll的名称,component后为资源文件相对于dll中的位置

<Application x:Class="WPFapp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WPFapp"
            >
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/styles;component/Style.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

这样就可以全局的访问资源字典中的资源了。

注意:在wpf程序中遵循就近查找原则,当定义的资源没有key时会默认覆盖所有TargetType类型的控件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值