当前位置: 首页 > news >正文

[C#] WPF - 自定义样式(Slider篇)

一、定义样式

在App.xaml里面定义样式:

<Applicationx:Class="WpfApp.StudySlider.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:WpfApp.StudySlider"StartupUri="MainWindow.xaml"><Application.Resources><Style x:Key="SliderDecreaseRepeatButton" TargetType="RepeatButton"><Setter Property="Focusable" Value="False" /><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="RepeatButton"><Border Height="15" Background="Transparent"><BorderHeight="6"Background="#2182FF"CornerRadius="3 0 0 3" /></Border></ControlTemplate></Setter.Value></Setter></Style><Style x:Key="SliderIncreaseRepeatButton" TargetType="RepeatButton"><Setter Property="Focusable" Value="False" /><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="RepeatButton"><Border Height="15" Background="Transparent"><BorderHeight="6"Background="#B3AAAC"CornerRadius="0 3 3 0" /></Border></ControlTemplate></Setter.Value></Setter></Style><Style x:Key="SliderThumb" TargetType="Thumb"><Setter Property="Focusable" Value="False" /><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="Thumb"><BorderWidth="15"Height="15"Margin="-1,0"Background="#FFD9D3E8"CornerRadius="10" /><ControlTemplate.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Cursor" Value="Hand" /></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style><Style x:Key="CustomSlider" TargetType="Slider"><Setter Property="Focusable" Value="False" /><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="Slider"><Grid><BorderHeight="6"Background="#EBEDF2"CornerRadius="4" /><Track Name="PART_Track"><Track.DecreaseRepeatButton><RepeatButton Command="Slider.DecreaseLarge" Style="{StaticResource SliderDecreaseRepeatButton}" /></Track.DecreaseRepeatButton><Track.IncreaseRepeatButton><RepeatButton Command="Slider.IncreaseLarge" Style="{StaticResource SliderIncreaseRepeatButton}" /></Track.IncreaseRepeatButton><Track.Thumb><Thumb Style="{StaticResource SliderThumb}" /></Track.Thumb></Track></Grid></ControlTemplate></Setter.Value></Setter></Style></Application.Resources>
</Application>

二、应用样式

在MainWindow.xaml里面使用样式:

<Windowx:Class="WpfApp.StudySlider.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:local="clr-namespace:WpfApp.StudySlider"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"Title="MainWindow"Width="400"Height="300"mc:Ignorable="d"><Grid><Grid.RowDefinitions><RowDefinition Height="*" /><RowDefinition Height="*" /></Grid.RowDefinitions><GroupBox Grid.Row="0" Header="默认样式"><SliderMaximum="100"Minimum="0"Value="30" /></GroupBox><GroupBox Grid.Row="1" Header="自定义样式"><SliderMaximum="100"Minimum="0"Style="{StaticResource CustomSlider}"Value="30" /></GroupBox></Grid>
</Window>

三、运行效果

http://www.lryc.cn/news/578531.html

相关文章:

  • 腾讯 iOA 零信任产品:安全远程访问的革新者
  • 数据结构day4——栈
  • 回转体水下航行器简单运动控制的奥秘:PID 控制和水动力方程的运用
  • 信息安全相关算法
  • 蓝牙音频传输协议深度解析:A2DP、HFP、AVRCP 对比与面试核心考点
  • 【机器学习2】正则化regularizaiton(降低模型过拟合)
  • 【cv视觉】标注工具的使用和数据集的创建
  • 2.SQL语句执行慢,如何分析
  • 07CSRF 漏洞保护
  • 事件监听器 + 回调处理器的事件循环系统
  • OpenCV CUDA模块设备层-----二值化阈值操作函数thresh_binary_func()
  • 设计模式精讲 Day 21:策略模式(Strategy Pattern)
  • 【STM32】 STM32低功耗模式详解:睡眠模式与唤醒机制【待测试】
  • 单元测试详解
  • 记录一个 Linux中脚本无法执行的问题
  • 构建淘宝评论监控系统:API 接口开发与实时数据采集教程
  • Camera相机人脸识别系列专题分析之十五:人脸特征检测FFD算法之libcvface_api.so算法API详细注释解析
  • Docker制作python环境
  • C++ 11 中 condition_variable 的探索与实践
  • 当足球遇上AI:赛事分析、伤病预测与智能裁判的崛起
  • postman入门篇
  • RabbitMQ - SpringAMQP及Work模型
  • k8s将service的IP对应的不同端口分配到不同的pod上
  • Vue 3 中的 `h` 函数详解
  • CAD文件处理控件Aspose.CAD教程:使用 Python 将绘图转换为 Photoshop
  • 【Python】字典get方法介绍
  • 面试拷打-20250701
  • 计网学习笔记第1章 计算机网络体系结构(灰灰题库)
  • 论文阅读笔记 NoPoSplat
  • 笔记/计算机网络