当前位置: 首页 > 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.lqws.cn/news/593101.html

相关文章:

  • WPF学习(三)
  • 08跨域
  • vue-i18n+vscode+vue 多语言使用
  • Mac 部署Latex OCR并优化体验(打包成App并支持全局快捷键)
  • WebSocket技术全面解析:从历史到实践
  • (Python)Python基础语法介绍(二)(Python基础教学)
  • 老年护理实训室建设方案:打造沉浸式护理实训环境
  • pulseaudio实现音频的网络传输
  • Wireshark TS | 诡异的光猫网络问题
  • 中心效应:多中心临床试验的关键考量
  • Selector组件组件
  • sentinel滑动窗口及熔断限流实现原理
  • STM32作为主机识别鼠标键盘
  • Gradio全解13——MCP协议详解(5)——Python包命令:uv与uvx实战
  • Easy Window UI设计器 - 图表组件 10秒完成UI效果
  • Xposed框架深度解析:Android系统级Hook实战指南
  • Flask+LayUI开发手记(十):构建统一的选项集合服务
  • QGIS合并、拆分SHP文件
  • 深入理解栈的合法弹出序列验证算法
  • docusaurus初步体验
  • Bootstrap 安装使用教程
  • 多bin技术:为LoRa项目赋能的高效远程升级方案
  • OpenCV CUDA模块设备层-----双曲正切函数tanh()
  • Terraform Helm:微服务基础设施即代码
  • 《UE5_C++多人TPS完整教程》学习笔记39 ——《P40 远程过程调用(Remote Procedure Calls)》
  • LabVIEW自动扶梯振动监测
  • RabbitMQ简单消息发送
  • Node.js与Express框架的深度整合
  • beego打包发布到Centos系统及国产麒麟系统完整教程
  • react-数据Mock实现——json-server