本人准备学习WPF的知识,记录下自己写的例子和代码,便于自己以后的查询和使用。
如果能够帮助大家,就更加的荣幸了。
目前学习的书是《WPF揭秘》,用根据这本书学习。如有需要请支持正版。
1. 按钮
基本的按钮控件是一个可以被单击但不能被双击的内容控件。这种行为其实被一个叫作ButtonBase 的抽象类所捕获,其他许多不同的控件也都从ButtonBase 抽象类继承。
ButtonBase 类包含了Click 事件和用来定义单击含义的逻辑。为此ButtonBase类定义了布尔类型的IsPressed 属性。
一个简单的按钮程序:
<Button Height="30" Width="100">
OK
<Button.ToolTip>
<ToolTip>
Clicking this will submit your request.
</ToolTip>
</Button.ToolTip>
</Button>
ToolTip:ToolTip 控件把它的内容放在一个浮动框中,当把鼠标移过与之关联的控件时,就会显示ToolTip 的内容,鼠标移开以后内容会消失。
ToggleButton:
ToggleButton 是一种在点击时可以保留其状态的“粘性”按钮(它也没有Button 控件的取消行为以及默认行为)。第1 次点击它的时候,IsChecked 属性会被设为true;再点击一次,就被设为了false。ToggleButton的默认外观和Button 以及RepeatButton 完全一样。
ToggleButton 还有一个IsThreeState 属性,如果把它设为true 的话,IsChecked 就会有3 种值:true、false或者null。事实上,IsChecked 是Nullable<Boolean>类型的。
CheckBox:
可以在被点击时记录选中(checked)或者未选中(unchecked)状态。
·它支持3 种状态模式,分别为选中(checked)、不确定(indeterminate)、未选中(unche- cked)。
RadioButton:
种从ToggleButton 继承过来的控件,但它的特殊在于支持互斥性。当多个RadioButton被放在一个组里,一次只有一个可以被选中,把一个RadioButton 选中就会自动把组中其他所有的RadioButton 设为不选中。
<StackPanel>
<RadioButton>Option1</RadioButton>
<RadioButton>Option2</RadioButton>
<RadioButton>Option3</RadioButton>
</StackPanel>
可以添加GroupName分组。
<StackPanel>
<RadioButton GroupName="A">Option1</RadioButton>
<RadioButton GroupName="A">Option2</RadioButton>
<RadioButton GroupName="B">OptionA</RadioButton>
<RadioButton GroupName="B">OptionB</RadioButton>
</StackPanel>
先举一个office风格的例子,具体内容可以慢慢理解。
<CheckBox>
CheckBox
<CheckBox.ToolTip>
<StackPanel>
<Label FontWeight="Bold" Background="Blue" Foreground="White">
The CheckBox
</Label>
<TextBlock Padding="10" TextWrapping="WrapWithOverflow" Width="200">
Checkbox is a familiar control.
</TextBlock>
<Line Stroke="Black" StrokeThickness="1" X2="200" />
<StackPanel Orientation="Horizontal">
<Image Margin="2" Source="help.gif" />
<Label FontWeight="Bold">Press F1 for more help</Label>
</StackPanel>
</StackPanel>
</CheckBox.ToolTip>
</CheckBox>