Silverlight编程之Silverlight制作简易工具条控件(1)(3)
第五步,让工具条内的按钮互斥:
1、工具条内的四个按钮分别代表四种状态:
BIN:二进制
OCT:八进制
BCD:十进制
HEX:十六进制
为了方便后面写程序,这里声明一个枚举来表示这四种状态。
在【BitLibrary】项目上单击鼠标右键,选择【添加】à【新建项】,生成一个新类,命名为:“SystemState.cs”。在其中输入如下代码:
1 public enum SystemState
2 {
3 None,
4 Bin,
5 Oct,
6 Bcd,
7 Hex
8 }
9
2、给按钮声明一个SystemState属性,用于保存这个按钮所属状态。打开SpeedButton.cs文件,并在SpeedButton添加一个State属性:
1 public SystemState State
2 {
3 get;
4 set;
5 }
3、打开ToolBar.xaml.cs文件,更改代码如下:
1 public partial class ToolBar : UserControl
2 { //声明一个路由事件SelectionChange,用于在状态改变时通知用户
3 public event RoutedEventHandler SelectionChange;
4 public ToolBar()
5 {
6 InitializeComponent();
7 }
8
9 private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
10 { //设置每个按钮所属状态
11 btnBin.State = SystemState.Bin;
12 btnOct.State = SystemState.Oct;
13 btnBcd.State = SystemState.Bcd;
14 btnHex.State = SystemState.Hex;
15 //将每个按钮的MouseLeftButtonDown事件连接至ToolBar的btn_MouseLeftButtonDown方法之中
16 btnBin.MouseLeftButtonDown += new MouseButtonEventHandler(btn_MouseLeftButtonDown);
17 btnOct.MouseLeftButtonDown += new MouseButtonEventHandler(btn_MouseLeftButtonDown);
18 btnBcd.MouseLeftButtonDown += new MouseButtonEventHandler(btn_MouseLeftButtonDown);
19 btnHex.MouseLeftButtonDown += new MouseButtonEventHandler(btn_MouseLeftButtonDown);
20 }
21 private SystemState toolState = SystemState.None;
22 public SystemState ToolState //表示工具条当前所属状态
23 {
24 get { return toolState; }
25 set
26 {
27 if (toolState == value) return;
28 toolState = value;
29 //恢复每个按钮的正常状态
30 btnBin.IsPress = false;
31 btnOct.IsPress = false;
32 btnBcd.IsPress = false;
33 btnHex.IsPress = false;
34 //根据当前状态使相应按钮处于按下状态
35 switch (toolState)
36 {
37 case SystemState.Bin:
38 btnBin.IsPress = true;
39 break;
40 case SystemState.Oct:
41 btnOct.IsPress = true;
42 break;
43 case SystemState.Bcd:
44 btnBcd.IsPress = true;
45 break;
46 case SystemState.Hex:
47 btnHex.IsPress = true;
48 break;
49 }
50 //触发SelectionChange事件
51 if (SelectionChange != null)
52 {
53 SelectionChange(this, new RoutedEventArgs());
54 }
55 }
56 }
57 //所有按钮单击时都会连接至这个方法
58 void btn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
59 { //通过设置ToolState属性以执行set访问器代码
60 this.ToolState = ((SpeedButton)sender).State;
61 }
62 }
63
这里我们把所有按钮的单击事件连接至ToolBar类里的btn_MouseLeftButtonDown()方法之中,这样按钮的单击事件就可以传递至ToolBar工具条了。呵呵,这一招是看ToolKit源码时学到的,这方法够巧妙。
现在可以运行程序,可以发现,当按下一个按钮时,之前被按下的按钮会自动弹起。
4、为了测试我们按下按钮时是否可以马上知道工具条当前状态,更改MainPage.xaml代码如下:
<UserControl x:Class="BitCalculator.MainPage"
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:src="clr-namespace:BitLibrary;assembly=BitLibrary"
mc:Ignorable="d" d:DesignWidth="120" d:DesignHeight="70">
<StackPanel x:Name="LayoutRoot">
<src:ToolBar x:Name="toolBar" Grid.Row="1" SelectionChange="toolBar_SelectionChange"/>
<TextBlock x:Name="txtMsg" Width="120" TextAlignment="Center" FontSize="13" Margin="10"/>
</StackPanel>
</UserControl>
打开MainPage.xaml.cs文件,并更改代码如下:
1 public partial class MainPage : UserControl
2 {
3 public MainPage()
4 {
5 InitializeComponent();
6 }
7
8 private void toolBar_SelectionChange(object sender, RoutedEventArgs e)
9 {
10 txtMsg.Text = "你按下了" + toolBar.ToolState.ToString() + "按钮";
11 }
12 }
现在就完成了如文章开头所演示的效果