首页 > 其他分享 >wpf关于Resource,Style的定义与引用,滑动按钮

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

时间:2024-07-02 16:56:58浏览次数:3  
标签:控件 Style Resource 样式 ToggleButton wpf 资源 字典

当我们使用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类型的控件

标签:控件,Style,Resource,样式,ToggleButton,wpf,资源,字典
From: https://blog.csdn.net/balabala___a/article/details/140129478

相关文章

  • WPF 分隔栏分割窗体简单测试
    XAML:<Windowx:Class="WpfApp3.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.micro......
  • WPF 中 StackPanel 控件的可视化 Visibility.Collapsed 控件元素会自动前移
    XAML:<Grid><Grid.RowDefinitions><RowDefinition/><RowDefinitionHeight="Auto"/></Grid.RowDefinitions><StackPanelGrid.Row="0"><TextBoxx:Name="txb_001&quo......
  • IntelliJ IDEA java maven项目读取配置文件信息 java.util.ResourceBundle 方式
    一、在main目录下新建resources目录并将其设为资源文件目录  创建config.properties文件二、在pom.xml中添加下面代码 只这样打包后jar才能有配置文件<resources><resource><filtering>true</filtering><directory>src/main/......
  • 开发界的良心评测文章,告诉你WPF 与 WinForms 对比:性能、开发难度、特点及操作系统支持
    引言WindowsPresentationFoundation(WPF)和WindowsForms(WinForms)是微软提供的两种用于构建Windows桌面应用程序的框架。尽管它们都有助于开发高效的桌面应用程序,但在性能、开发难度、特点和操作系统支持方面存在显著差异。本文将详细比较WPF和WinForms,以帮助开发者选择......
  • 基于WPF+Halcon开发的机器视觉框架,参考easyvision开发
    基于WPF+Halcon开发的机器视觉框架,参考easyvision开发50多个模块,WPF+Halcon开发,是非常不错的学习框架,需要什么功能可以自己添加开发,插件式开发非常方便,代码开源可二次开发。整套源码,可学习参考,也可略做修改,用在自己的项目上。基于WPF+Halcon开发的机器视觉框架运行界面......
  • Simple WPF: WPF 透明窗体和鼠标事件穿透
    一个自定义WPF窗体的解决方案,借鉴了吕毅老师的WPF制作高性能的透明背景的异形窗口一文,并在此基础上增加了鼠标穿透的功能。可以使得透明窗体的鼠标事件穿透到下层,在下层窗体中响应。这个方法不一定是制作WPF透明窗体最合适的方法,请各路大大不要喷。完整代码地址:Github一、去除......
  • WPF资源的使用
    目录本地文件资源的使用图片使用设置图片属性:本地图片加载:直接加载url使用,使用网络图片跨程序集使用:常用方法音视频使用字体图标文件的使用字体图标文件的操作与合并资源字典的使用资源字典切换案例本地文件资源的使用图片使用图片有两种使用方式设置图片属性......
  • WPF ResourceDictionary ResourceDictionary.MergedDictionaries
    1.Addresourcedictionary,Brushes.xaml<ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><LinearGradientBrush......
  • WPF reference project
    //xaml<Windowx:Class="WpfApp1.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.micr......
  • 前端vue/react通用工程化eslint prettier stylelint husky项目规范(新手入门详细教程)
    前言本文章内的项目基于vite+react+ts搭建,但通篇并未涉及react的东西,所以可以通用。适合新手入门的工程化项目规范,最小化的完成代码规范和git提交规范,开发工具使用vscode。为什么是最小化?本意是为了让大家都能看懂入手这些规范,很多文章中,都长篇进行自定义的配置,其......