WPF开发者QQ群: 340500857  | 微信群 -> 进入公众号主页 加入组织

 前言,接着上一篇圆形进度条。

欢迎转发、分享、点赞、在看,谢谢~。

01

效果预览

效果预览(更多效果请下载源码体验):

02

代码如下

一、DashboardControl.cs 代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using WpfDashboard.Models;namespace WpfDashboard
{public class DashboardControl : ProgressBar{public DashboardControl(){this.ValueChanged += CircularProgressBar_ValueChanged;}void CircularProgressBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e){DashboardControl bar = sender as DashboardControl;double currentAngle = bar.Angle;double targetAngle = e.NewValue / bar.Maximum * 180;Angle = targetAngle;if (ScaleArray == null)ArrayList();var count = Convert.ToInt32(Angle / (180 / ScaleNum));ScaleArray.ToList().ForEach(y =>{y.Background = Brushes.White;});Brush color = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF19DCF0"));ScaleArray.Where(x => x.Index <= count).ToList().ForEach(y =>{y.Background = color;});}public double Angle{get { return (double)GetValue(AngleProperty); }set { SetValue(AngleProperty, value); }}public static readonly DependencyProperty AngleProperty =DependencyProperty.Register("Angle", typeof(double), typeof(DashboardControl), new PropertyMetadata(0.0));public IList<ScaleModel> ScaleArray{get { return (IList<ScaleModel>)GetValue(ScaleArrayProperty); }private set { SetValue(ScaleArrayProperty, value); }}public static readonly DependencyProperty ScaleArrayProperty =DependencyProperty.Register("ScaleArray", typeof(IList<ScaleModel>), typeof(DashboardControl), new PropertyMetadata(null));public int ScaleNum{get { return (int)GetValue(ScaleNumProperty); }set { SetValue(ScaleNumProperty, value); }}public static readonly DependencyProperty ScaleNumProperty =DependencyProperty.Register("ScaleNum", typeof(int), typeof(DashboardControl), new PropertyMetadata(18));void ArrayList(){List<ScaleModel> shortticks = new List<ScaleModel>();for (int i = 0; i < ScaleNum; i++){shortticks.Add(new ScaleModel { Index = i, Background = Brushes.White });}this.ScaleArray = shortticks;}}
}

二、App.xaml 代码如下

<Application x:Class="WpfDashboard.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:ec="http://schemas.microsoft.com/expression/2010/controls" xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"xmlns:local="clr-namespace:WpfDashboard"StartupUri="MainWindow.xaml"><Application.Resources><LinearGradientBrush x:Key="NormalBrush" EndPoint="0.5,1" StartPoint="0.5,0"><GradientStop Color="#FF164DA7"/><GradientStop Color="#FF19DCF0" Offset="1"/></LinearGradientBrush><Style TargetType="local:DashboardControl"><Setter Property="Maximum" Value="100"/><Setter Property="Background" Value="#252525"/><Setter Property="Width" Value="200"/><Setter Property="Height" Value="200"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="local:DashboardControl"><Viewbox><Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"Background="{TemplateBinding Background}"RenderTransformOrigin="0.5,0.5"><Grid.RenderTransform><TransformGroup><RotateTransform Angle="-90"/></TransformGroup></Grid.RenderTransform><ed:Arc  ArcThickness="8" ArcThicknessUnit="Pixel" Fill="White"RenderTransformOrigin="0.5,0.5" StartAngle="0"EndAngle="180"Stretch="None"Margin="10"/><ed:Arc x:Name="PART_PathBackground" Margin="24" ArcThickness="0" ArcThicknessUnit="Pixel"EndAngle="180"StartAngle="0"Stretch="None" /><ed:Arc ArcThickness="8" ArcThicknessUnit="Pixel" Fill="{StaticResource NormalBrush}"StartAngle="0"EndAngle="{Binding Angle, RelativeSource={RelativeSource FindAncestor, AncestorType=ProgressBar}}"Stretch="None" Margin="10"/><ec:PathListBox IsHitTestVisible="False"ItemsSource="{Binding ScaleArray,RelativeSource={RelativeSource FindAncestor,AncestorType=ProgressBar}}"><ec:PathListBox.ItemTemplate><DataTemplate><Border Width="2" Height="8" Background="{Binding Background}" SnapsToDevicePixels="True"UseLayoutRounding="True" /></DataTemplate></ec:PathListBox.ItemTemplate><ec:PathListBox.LayoutPaths><ec:LayoutPath Distribution="Even" Orientation="OrientToPath"SourceElement="{Binding ElementName=PART_PathBackground}" /></ec:PathListBox.LayoutPaths></ec:PathListBox><Border RenderTransformOrigin="0.5,0.5" Margin="30,0,0,0"><Border.RenderTransform><TransformGroup><RotateTransform Angle="90"/></TransformGroup></Border.RenderTransform><TextBlock Foreground="{StaticResource NormalBrush}"FontSize="40"HorizontalAlignment="Center" VerticalAlignment="Center"Text="{Binding Path=Value, StringFormat={}{0}%, RelativeSource={RelativeSource TemplatedParent}}"FontWeight="Bold" FontFamily="Agency FB"/></Border></Grid></Viewbox></ControlTemplate></Setter.Value></Setter>
</Style></Application.Resources>
</Application>

三、MainWindow.xaml 代码如下

<Window x:Class="WpfDashboard.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfDashboard"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><StackPanel VerticalAlignment="Center"><local:DashboardControl Value="{Binding ElementName=CirularSlider,Path=Value}"/><Slider Minimum="0" Maximum="100" Margin="0,10"x:Name="CirularSlider" IsSnapToTickEnabled="True"VerticalAlignment="Center" Value="10" Width="220"/></StackPanel></Grid>
</Window>

源码地址

github:https://github.com/yanjinhuagood/WPFDevelopers.git

gitee:https://gitee.com/yanjinhua/WPFDevelopers.git

WPF开发者QQ群: 340500857

blogs: https://www.cnblogs.com/yanjinhua

Github:https://github.com/yanjinhuagood

出处:https://www.cnblogs.com/yanjinhua

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

转载请著名作者 出处 https://github.com/yanjinhuagood

WPF实现仪表盘(刻度跟随)相关推荐

  1. WPF自定义仪表盘控件

    WPF自定义仪表盘控件 一.前言 二.功能实现 一.前言 在学习和工作中使用WPF时,都离不开自定义控件的使用,今天分享一个自己在学习过程中使用到的一个自定义仪表盘控件,感觉挺不错的,在这里分享给大家 ...

  2. 仪表盘刻度调整_惊艳了我的仪表盘 那些年我遇到最美的“面孔”

    文| G哥 汽仪表盘上的信息可以让车主们了解自己座驾的状况,出彩的仪表盘设计甚至能让消费者购车时对这款车一见倾心,回顾过往曾经惊艳到我们的那些酷炫仪表盘,那些面孔你是否还依稀还记得-- 汽车的什么地方 ...

  3. echarts 仪表盘刻度线随进度条改变颜色

    示例图 option = {series: [// 最小{type: 'gauge',radius: '15%', // 位置center: ['50%', '60%'],min: 0,max: 10 ...

  4. Echarts仪表盘刻度线和标签配置

    该案例只做参考 步骤: 1.首先设置好盒子的宽高 demo页面 css页面 2. 该方法在进入页面时加载 3. methods方法: // 待处理阈值实时监听 handleRefGauge1() {c ...

  5. 看看这套WPF开源基础控件库:WPFDevelopers

    此项目包含了 微信公众号 < WPF开发者> 日常开发分享,欢迎Star. 运行环境 Visual Studio 2019,dotNet Framework 4.0 SDK 欢迎关注微信公 ...

  6. vue+echarts实现多个仪表盘图表

    vue+echarts实现多个仪表盘图表 根据echarts官网单个仪表盘修改成多个仪表盘,代码如下: <template><div class="app-containe ...

  7. ECharts仪表盘(详细示例——附有具体注释)

    单仪表盘图表效果如下: 具体代码如下: <!DOCTYPE html> <html><head><meta charset="UTF-8" ...

  8. Echart 仪表盘 样式调整

    option5 = {series: [{type: 'gauge',progress: {show: true,width: 2},axisLine: {lineStyle: {width: 4,/ ...

  9. 如何用echarts实现颜色渐变半圆形仪表盘

    1.代码: <!DOCTYPE html> <html lang="en"> <head><meta charset="utf- ...

最新文章

  1. 为什么S/4HANA的销售订单创建会触发生产订单的创建
  2. netca 遇到TNS-04612: XXX 的 RHS 为空
  3. 从零开始实现ASP.NET Core MVC的插件式开发(六) - 如何加载插件引用
  4. Silverlight.XNA(C#)跨平台3D游戏研发手记:(一)差集运算在SLG战斗范围设定中的应用...
  5. ASP.NET母版页和内容页之间如何互相传值?
  6. 2008R2 RDS架构
  7. 8月总庆祝:点击过百万,升级V8,进入前300名
  8. 多进程-multiprocess
  9. DiskGenius 对 U 盘分区操作后保存更改时报错:保存分区表时出现错误20000011e no such partition
  10. 高动态范围图像(HDR)处理
  11. 第二十章 幻读是什么,幻读有什么问题?
  12. RxSwift 学习笔记
  13. Glide加载图片会出现淡绿色的背景
  14. LC365. 水壶问题
  15. Windows10 U盘无法格式化怎么办?U盘拒绝访问?
  16. php把搜索的结果导出,excel表格查找数据之后导出-如何将Excel查找全部后得到的内容Copy出来...
  17. S型函数:Sigmoid 函数
  18. MySQL调优系列:如何正确的使用MySQL索引
  19. Python入门系列(十一)一篇搞定python操作MySQL数据库
  20. win7如何设置通电自动开机_如何通过BIOS设置电脑接通电源就自动启动进入系统...

热门文章

  1. EqualLogic全攻略视频[(四)高级管理]
  2. [分享]我国食品行业ERP现状及分析解决方案
  3. jQuery 1.9+ 移除$.browser方法
  4. 每个人都要在自己的“时区”里找到自己的快乐
  5. springMVC3学习(十一)--文件上传CommonsMultipartFile
  6. STM32的FLASH ID加密
  7. JQuery框架2.位置属性|筛选方法|事件
  8. 使用qrcode类制作二维码
  9. 命令行打印文件树列表: tree 1
  10. C#深入.NET平台的软件系统分层开发