VU Meter Control in WPF

VUMeterControl.xaml:

VUMeterControl.xaml
1 <UserControl x:Class="WpfApplication1.VUMeterControl"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Width="20">
5 <StackPanel Name="panel" Background="Gray">
6 <Rectangle Margin="2" Fill="Red" Height="20"/>
7 <Rectangle Margin="2" Fill="Red" Height="20"/>
8 <Rectangle Margin="2" Fill="Red" Height="20"/>
9 <Rectangle Margin="2" Fill="Yellow" Height="20"/>
10 <Rectangle Margin="2" Fill="Yellow" Height="20"/>
11 <Rectangle Margin="2" Fill="Yellow" Height="20"/>
12 <Rectangle Margin="2" Fill="Yellow" Height="20"/>
13 <Rectangle Margin="2" Fill="Yellow" Height="20"/>
14 <Rectangle Margin="2" Fill="Green" Height="20"/>
15 <Rectangle Margin="2" Fill="Green" Height="20"/>
16 <Rectangle Margin="2" Fill="Green" Height="20"/>
17 <Rectangle Margin="2" Fill="Green" Height="20"/>
18 <Rectangle Margin="2" Fill="Green" Height="20"/>
19 <Rectangle Margin="2" Fill="Green" Height="20"/>
20 <Rectangle Margin="2" Fill="Green" Height="20"/>
21 </StackPanel>
22  </UserControl>
23  

VUMeterControl.xaml.cs:

VUMeterControl.xaml.cs
1 using System;
2  using System.Collections.Generic;
3  using System.Linq;
4  using System.Text;
5  using System.Windows;
6  using System.Windows.Controls;
7  using System.Windows.Data;
8  using System.Windows.Documents;
9  using System.Windows.Input;
10  using System.Windows.Media;
11  using System.Windows.Media.Imaging;
12  using System.Windows.Navigation;
13  using System.Windows.Shapes;
14
15  namespace WpfApplication1
16 {
17 /// <summary>
18 /// Interaction logic for VUMeterControl.xaml
19 /// </summary>
20   public partial class VUMeterControl : UserControl
21 {
22 public VUMeterControl()
23 {
24 InitializeComponent();
25 }
26
27 public int MaxValue
28 {
29 get { return (int)GetValue(MaxValueProperty); }
30 set { SetValue(MaxValueProperty, value); }
31 }
32 public static readonly DependencyProperty MaxValueProperty =
33 DependencyProperty.Register("MaxValue", typeof(int), typeof(VUMeterControl), new UIPropertyMetadata(100));
34
35 public int Value
36 {
37 get { return (int)GetValue(ValueProperty); }
38 set { SetValue(ValueProperty, value); }
39 }
40
41 public static readonly DependencyProperty ValueProperty =
42 DependencyProperty.Register("Value", typeof(int), typeof(VUMeterControl), new UIPropertyMetadata(0,
43 (sender, e) =>
44 {
45 VUMeterControl control = (VUMeterControl)sender;
46 int value = (int)e.NewValue;
47 for (int i = control.panel.Children.Count - 1; i >= 0; i--)
48 {
49 Rectangle rect = (Rectangle)control.panel.Children[i];
50 if (15 - i > value / (control.MaxValue / 15))
51 rect.Visibility = Visibility.Hidden;
52 else
53 rect.Visibility = Visibility.Visible;
54 }
55 })
56 );
57 }
58 }
59  

 

and the usage in the window:

1 <StackPanel>
2 <l:VUMeterControl x:Name="vuMeterControl"/>
3 <Slider Value="{Binding ElementName=vuMeterControl, Path=Value}" Maximum="100"/>
4 </StackPanel>

 

 

WPF QQ交流群: 113404016  欢迎加入
posted @ 2010-06-23 21:32  Jarrey  阅读(1424)  评论(0编辑  收藏  举报