这是我第二次为winforms问题提供WPF解决方案。
只需将我的代码复制并粘贴到文件 -> 新项目 -> WPF 应用程序中,然后亲自查看结果。
另请看一下这段代码到底有多简单(我使用的是随机值,因此您可以删除它并根据您的需要进行调整)。
我用的图(<Path/>
XAML 中的部分)对于仪表来说是不够的。我刚刚已经绘制了该路径,但我懒得创建新路径。您应该创建一个新绘图(我建议使用 Expression Blend)。但您可以看到正在应用的旋转及其运行速度。
using System;
using System.Threading;
using System.Windows;
using System.ComponentModel;
namespace WpfApplication4
{
public partial class Window2
{
public Window2()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
public class ViewModel: INotifyPropertyChanged
{
private double _value;
public double Value
{
get { return _value; }
set
{
_value = value;
NotifyPropertyChange("Value");
}
}
private int _speed = 100;
public int Speed
{
get { return _speed; }
set
{
_speed = value;
NotifyPropertyChange("Speed");
Timer.Change(0, value);
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChange(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private System.Threading.Timer Timer;
public ViewModel()
{
Rnd = new Random();
Timer = new Timer(x => Timer_Tick(), null, 0, Speed);
}
private void Timer_Tick()
{
Application.Current.Dispatcher.BeginInvoke((Action) (NewValue));
}
private Random Rnd;
private void NewValue()
{
Value = Value + (Rnd.Next(20) - 10);
}
}
}
XAML:
<Window x:Class="WpfApplication4.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" WindowState="Maximized">
<DockPanel>
<StackPanel DockPanel.Dock="Top">
<TextBlock Text="Delay (MS):" Margin="2"/>
<Slider Width="200" Minimum="100" SmallChange="1" LargeChange="10" Maximum="1500" Value="{Binding Speed}" Margin="2"/>
<TextBlock Text="Current Value:" Margin="2"/>
<TextBox Text="{Binding Value}" Margin="2"/>
</StackPanel>
<Path Data="M0.95991516,0.5 L73.257382,1.866724 90.763535,1.866724 90.763535,90.822725 66.430534,90.822725 66.430534,26.075016 0.5,24.828653 z" Fill="#FF506077" RenderTransformOrigin="0.861209625003783,0.507482926584064" Stretch="Fill" Stroke="Black">
<Path.LayoutTransform>
<TransformGroup>
<ScaleTransform ScaleY="1" ScaleX="-1"/>
<SkewTransform AngleY="0" AngleX="0"/>
<RotateTransform Angle="{Binding Value}" x:Name="Rotation"/>
<TranslateTransform/>
</TransformGroup>
</Path.LayoutTransform>
</Path>
</DockPanel>
</Window>