Silverlight编程之Silverlight制作简易工具条控件(1)(2)
4、 打开SpeedButton.cs文件,给SpeedButton类添加构造函数如下:
1 public class SpeedButton : ContentControl
2 {
3 public SpeedButton()
4 {
5 DefaultStyleKey = typeof(SpeedButton);
6 }
7 }
8
5、 修改MainPage.xaml文件,更改SpeedButton的声明如下:
<src:SpeedButton Content="HEX" />
6、运行程序,现在一个完整钮呈现在我们的面前,如下图所示。
当我们把鼠标指针移到按钮上方或按下按钮时无任何反应,这时需要对鼠标的事件进行处理,指定这些事件的可视状态。
第三步,给按钮添加鼠标响应事件:
虽然我们给按钮设定了模板,并在模板中指定了鼠标经过及按钮按下时的状态,但我们并没有在SpeedButton中指定何时呈现这些状态。下面处理SpeedButton的鼠标事件,并指定相应的可视状态。
打开SpeedButton.cs文件,并修改代码如下:
1 public class SpeedButton : ContentControl
2 {
3 public SpeedButton()
4 {
5 DefaultStyleKey = typeof(SpeedButton);
6 //指定鼠标事件方法
7 MouseEnter += new MouseEventHandler(SpeedButton_MouseEnter);
8 MouseLeave += new MouseEventHandler(SpeedButton_MouseLeave);
9 MouseLeftButtonDown += new MouseButtonEventHandler(SpeedButton_MouseLeftButtonDown);
10 }
11 private Boolean isPress = false; //指示按钮是否处于按下状态
12 public Boolean IsPress
13 {
14 get
15 {
16 return isPress;
17 }
18 set
19 {
20 if (IsPress == value) return;
21 isPress = value;
22 if (isPress)
23 { //将按钮显示为模板中的Pressed可视状态。
24 VisualStateManager.GoToState(this, "Pressed", true);
25 }
26 else
27 {
28 VisualStateManager.GoToState(this, "Normal", true);
29 }
30 }
31 }
32 void SpeedButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
33 {
34 IsPress = !IsPress;
35 }
36
37 void SpeedButton_MouseLeave(object sender, MouseEventArgs e)
38 {
39 if (!isPress)
40 {
41 VisualStateManager.GoToState(this, "Normal", true);
42 }
43 }
44
45 void SpeedButton_MouseEnter(object sender, MouseEventArgs e)
46 {
47 if (!isPress)
48 {
49 VisualStateManager.GoToState(this, "MouseOver", true);
50 }
51 }
52 }
53
运行程序,用鼠标单击按钮看看有何不同。至此,一个简单的SpeedButton制作完毕。
第四步,制作ToolBar控件:
工具按钮是有了,接下来开始制作工具条。工具条其实就是一个容器,里面存放着几个工具按钮,当单击其中一个按钮时,另一个处于按下状态的按钮会自动弹上来,并且可以随时知道工具条当前按下的是哪个按钮。工具条的技术难题在于,当点击按钮改变工具条的选中项时,工具条如何知道按钮被点击,并触发一个事件来通知用户呢?按钮虽然拥有MouseLeftButtonDown事件,但如何把这个事件传递给工具条呢?
最初的想法是制作一个通用的工具条,但说实话,水平还不够,Silverlight中的很多机制还未了解透彻。而且通用控件需要考虑的东西太多,光代码可能就比位运算器多很多。这次还是制作一个位运算器专用的控件吧,好处是代码非常少,也容易理解。等未来水平达到了再考虑制作通用控件吧。
1、在【BitLibrary】项目上单击鼠标右键,选择【添加】à【新建项】,添加一个Silverlight用户控件,并命名为“ToolBar.xaml”。更改ToolBar.xaml文件如下:
<UserControl x:Class="BitLibrary.ToolBar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:BitLibrary;assembly=BitLibrary">
<StackPanel x:Name="LayoutRoot" Height="30" Width="120" Loaded="LayoutRoot_Loaded"
Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Center">
<src:SpeedButton x:Name="btnBin" Content="BIN"/>
<src:SpeedButton x:Name="btnOct" Content="OCT"/>
<src:SpeedButton x:Name="btnBcd" Content="BCD"/>
<src:SpeedButton x:Name="btnHex" Content="HEX"/>
</StackPanel>
</UserControl>
ToolBar控件只是由一个StackPanel装载了四个SpeedButton控件,并且每个按钮都命了名,以方便在后台代码文件中调用。
接下来在Loaded="LayoutRoot_Loaded"上单击鼠标右键,在弹出菜单中选择【导航到事件处理程序】,这时自动打开ToolBar.xaml.cs文件,并自动生成了LayoutRoot_Loaded事件方法。
2、打开MainPage.xaml文件,并将<Grid>标签内的控件改为ToolBar:
<src:ToolBar/>
运行程序,这时带有四个SpeedButton的工具条呈现,单击每个按钮,可以发现当按下一个按钮时,其他按钮不会自动弹起。下面着手解决这个问题。