当前位置: 首页 > 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.dtcms.com/a/263898.html

相关文章:

  • 【Hive SQL优化完全指南:从0.x到4.x的性能进化之路】
  • c# IO密集型与CPU密集型任务详解,以及在异步编程中的使用示例
  • [2025CVPR]DE-GANs:一种高效的生成对抗网络
  • 微分几何、旋量理论、李群李代数、黎曼度量、微分流形、SE(3)、SO(3)
  • java微服务-linux单机CPU接近100%优化
  • Jenkins × 容器技术:构建未来DevOps生态的超级引擎
  • 插入排序解析
  • C++ dll lib 以及编译链接加载的底层机制
  • 【从历史数据分析英特尔该如何摆脱困境】
  • 跨境证券交易系统合规升级白皮书:全链路微秒风控+开源替代,护航7月程序化交易新规落地
  • 手工部署与自动化部署场景模拟及参考项目
  • 数据结构与算法 第二章 线性表
  • Disruptor架构哲学
  • 【算法 day13】LeetCode 110.平衡二叉树 | 257. 二叉树的所有路径| 404.左叶子之和 |222.完全二叉树的节点个数
  • 【Python】字典get方法介绍
  • C++中的虚函数与纯虚函数
  • 【PaddleOCR】快速集成 PP-OCRv5 的 Python 实战秘籍--- PaddleOCR实例化 OCR 对象的参数介绍
  • css函数写个loading动画 | css预编译scss使用
  • YOLOv11性能评估全解析:从理论到实战的指标指南
  • GitHub已破4.5w star,从“零样本”到“少样本”TTS,5秒克隆声音,冲击传统录音棚!
  • 智能客服的进化论:当服务遇见 AI 的化学反应
  • 数据结构:递归:斐波那契数列(Fibonacci Sequence)
  • Android布局管理器实战指南:从LinearLayout到ConstraintLayout的优化之旅
  • 计算机网络第十章——网络层
  • 基于Netty-WebSocket构建高性能实时通信服务
  • nginx的管理员启动,停止,重启
  • 前端处理跨域的4种方式
  • uniapp+vue写小程序页面,实现一张图片默认放大后,可以在容器内上下左右拖动查看
  • JavaScript 安装使用教程
  • Web3区块链有哪些岗位?