本章没什么新奇的东东。
1、按钮
设置按钮的外边距 btn.Margin; 内边距 btn.Padding
// 绑定按钮的Command功能,
btn.Command = ApplicationCommands.Parse
// 设定按钮的显示内容
btn.Content = ApplicationCommands.Parse.Text;
注:ApplicationCommands 类型公开了以下成员。
名称 说明
CancelPrint “取消打印”
Close “关闭”
ContextMenu “上下文菜单”、
Copy “复制”
CorrectionList “集合列表”
Cut “剪切”
Delete “删除”
Find “查找”
Help “帮助”
New “新建”
NotACommand 始终被忽略的命令。
Open “打开”
Paste “粘贴”
Print “打印”
PrintPreview “打印预览”
Properties “属性”
Redo “重复”
Replace “替换”
Save “保存”
SaveAs “另存为”
SelectAll “全选”
Stop “停止”
Undo “撤消”
ToggleButton:可切换状态的按钮控件
示例:利用ToggleButton设置窗口是否可以改变大小
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
namespace Petzold.ToggleTheButton
{
public class ToggleTheButton : Window
{
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new ToggleTheButton());
}
public ToggleTheButton()
{
Title = "Toggle the Button";
ToggleButton btn = new ToggleButton();
btn.Content = "Can _Resize";
btn.HorizontalAlignment = HorizontalAlignment.Center;
btn.VerticalAlignment = VerticalAlignment.Center;
btn.IsChecked = (ResizeMode == esizeMode.CanResize);
btn.Checked += ButtonOnChecked;
btn.Unchecked += ButtonOnChecked;
Content = btn;
}
void ButtonOnChecked(object sender, RoutedEventArgs args)
{
ToggleButton btn = sender as ToggleButton;
// btn.IsChecked必须经过bool转换
ResizeMode = (bool)btn.IsChecked ? ResizeMode.CanResize : ResizeMode.NoResize;
}
}
}
Binding
示例:把ToggleButton的IsCheckedProperty与窗口Topmost绑定
namespace Petzold.BindTheButton
{
public class BindTheButton : Window
{
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new BindTheButton());
}
public BindTheButton()
{
Title = "Bind theButton";
ToggleButton btn = new ToggleButton();
btn.Content = "Make _Topmost";
btn.HorizontalAlignment = HorizontalAlignment.Center;
btn.VerticalAlignment = VerticalAlignment.Center;
btn.SetBinding(ToggleButton.IsCheckedProperty, "Topmost");
btn.DataContext = this;
Content = btn;
ToolTip tip = new ToolTip();
tip.Content = "Toggle the button on to make " +
"the window topmost on the desktop";
btn.ToolTip = tip;
}
}
}
绑定或许是WPF中最重要的内容了,用好了确实很方便,特别是在XAML里设置绑定,但刚接触WPF时不明白Binding的机制很容易头晕,
在Programming WPF这本书或包包的博客(http://www.cnblogs.com/Jax/archive/2008/04/15/1155135.html)里有关于Binding的详细介绍。