WPF概述
WPF简介
Windows Presentation Foundation 新一代图形用户界面开发框架
- 统一的编程模型
- 与分辨率无关
- 硬件加速技术
- 声明式编程:引入XAML语言,将界面开发与后台逻辑开发很好的分开,降低耦合度。
- 易于部署
WPF 开发环境搭建
WPF开发常用的辅助工具:
KAXAML 下载地址:http://www.kaxaml.com/
Blend 下载地址:http://msdn.microsoft.com/zh-cn/
<Button Content="Click Me" Height="30" Width="120"
Click="Button_Click"></Button>
MessageBox.Show("hello wpf");
XAML语言介绍
可扩展应用程序标记语言
- 定义应用程序的界面元素
- 显示的声明WPF资源(样式、模板、动画等)
- 可扩展性(自定义UI控件)
- 集中关注界面的设计和实现
XAML命名空间以及命名空间映射到程序集
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<!--
xaml命名空间的语法
xmlns[:可选映射前缀]="命名空间描述"
自定义类或程序集映射语法
xmlns:[映射前缀]="clr-namespace:[命名空间];assembly=[程序集名称]"
映射到当前程序集的语法
xmlns:[映射前缀]="clr-namespace:[命名空间]"
-->
XAML命名空间知识点
- 命名空间语法格式:xmlns[:映射前缀]=“名称空间”
- 默认命名空间只能有一个
- 自定义类映射语法:
xmlns:[映射前缀]=“clr-namespace:[命名空间];assembly=[程序集名称]” - X:Class属性的意义
WPF常用控件
WPF常用控件分类及介绍
控件:图形化用户界面中用来展示数据,以及能够响应用户操作的UI元素
WPF文本类型控件
文本控件:主要是用来显示以及编辑文本。
- TextBlock
<StackPanel >
<!--通用属性-->
<TextBlock Text="殷川少侠" FontFamily="宋体" FontSize="20" FontWeight="Bold" Foreground="Red"></TextBlock>
<!--专有属性1:是否自动折行-->
<TextBlock TextWrapping="Wrap">
商水县属周口市,位于河南省东南部,周口市西南部,西邻召陵区,南连上蔡县,东接项城市,北靠川汇区,西北、东北与西华、淮阳区隔沙河相望。商水县城位于新城区街道办事处与周口市川汇区连接,有大、小3路公交车互通。
</TextBlock>
<!--专有属性2:对齐方式-->
<TextBlock TextAlignment="Center">
星辰大海
</TextBlock>
<!--专有属性3:是否对显示文本进行裁剪-->
<TextBlock TextTrimming="CharacterEllipsis">
星辰大海,网络流行词,意思是我们有很远大的目标。因为“星辰”代表遥远和未知,而“大海”表示宏大无比,无边无际。
</TextBlock>
</StackPanel>
- TextBox
<Grid>
<TextBox Margin="10" Height="100" VerticalAlignment="Top"
Text="hhh"
TextWrapping="Wrap"
FontFamily="宋体" FontSize="12" FontWeight="Bold" Foreground="Blue"
AcceptsReturn="True" AcceptsTab="True"
TextChanged="TextBox_TextChanged"
></TextBox>
</Grid>
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
MessageBox.Show((sender as TextBox).Text);
}
- PasswordBox
<Grid>
<PasswordBox Margin="10" Height="25" Width="80" Password="" PasswordChar="s"
PasswordChanged="PasswordBox_PasswordChanged"></PasswordBox>
</Grid>
private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
{
MessageBox.Show((sender as PasswordBox).Password);
}
- RichTextBox
<Grid>
<RichTextBox Height="300" Margin="10">
<FlowDocument>
<Paragraph>
<Run>
颍川
</Run>
</Paragraph>
<BlockUIContainer>
<Image Source="照明光源.png"></Image>
</BlockUIContainer>
</FlowDocument>
</RichTextBox>
</Grid>
WPF内容控件
内容控件:只允许包含单一元素作为控件
按钮类型内容控件:
- Button
<Grid>
<StackPanel>
<Button Content="normal button" Width="100" Height="20" ></Button>
<Button Width="100" Height="20" >normal button</Button>
<Button Width="100" Height="20">
<Button.Content>
normal button
</Button.Content>
</Button>
</StackPanel>
<Button Width="100" Height="40">
<Button.Content>
<StackPanel>
<TextBlock>normal button 1</TextBlock>
<TextBlock>normal button 1</TextBlock>
</StackPanel>
</Button.Content>
</Button>
</Grid>
<Grid>
<Button Content="normal button" Width="100" Height="20"
Click="Button_Click"></Button>
</Grid>
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("update data success!");
}
- RadioButton : ToggleButton
只能选中同一分组的其中一个状态,类似于单选。
<StackPanel>
<RadioButton GroupName="sex">男</RadioButton>
<RadioButton GroupName="sex">女</RadioButton>
<RadioButton GroupName="type">人</RadioButton>
<RadioButton GroupName="type" >妖</RadioButton>
<RadioButton GroupName="type">神</RadioButton>
</StackPanel>
- CheckBox : ToggleButton
<StackPanel>
<CheckBox IsThreeState="True" Checked="CheckBox_Checked"
Unchecked="CheckBox_Unchecked"
Indeterminate="CheckBox_Indeterminate">CheckBox</CheckBox>
</StackPanel>
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
MessageBox.Show("Checked");
}
private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
MessageBox.Show("Unchecked");
}
private void CheckBox_Indeterminate(object sender, RoutedEventArgs e)
{
MessageBox.Show("Indeterminate");
}
- ToggleButton
在三种状态之间循环切换,并触发各状态所对应的事件
<StackPanel>
<ToggleButton IsThreeState="True" Click="ToggleButton_Click"
Checked="ToggleButton_Checked" Unchecked="ToggleButton_Unchecked"
Indeterminate="ToggleButton_Indeterminate">ToggleButton</ToggleButton>
</StackPanel>
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
var flag = (sender as ToggleButton).IsChecked;
if (flag.HasValue)
{
if (flag.Value)
{
MessageBox.Show("True");
}
else
{
MessageBox.Show("False");
}
}
else
{
MessageBox.Show("Indeteminate");
}
}
private void ToggleButton_Checked(object sender, RoutedEventArgs e)
{
MessageBox.Show("Checked");
}
private void ToggleButton_Unchecked(object sender, RoutedEventArgs e)
{
MessageBox.Show("Unchecked");
}
private void ToggleButton_Indeterminate(object sender, RoutedEventArgs e)
{
MessageBox.Show("Indeterminate");
}
- RepeatButton
点击后维持不放,连续触发click事件
int count = 0;
private void RepeatButton_Click(object sender, RoutedEventArgs e)
{
count++;
(sender as RepeatButton).Content = string.Format("RepeatButton{0}", count);
}
<StackPanel>
<RepeatButton Delay="2000" Interval="1000"
Click="RepeatButton_Click">RepeatButton</RepeatButton>
</StackPanel>
其他内容控件:
- Label
<StackPanel>
<Label Target="{Binding ElementName=tb}">_Lable</Label>
<Label>
<Button>ButtonLable1</Button>
</Label>
<TextBox Name="tb"></TextBox>
</StackPanel>
- GrouBox
<StackPanel>
<GroupBox Header="性别">
<StackPanel>
<RadioButton>男</RadioButton>
<RadioButton>女</RadioButton>
</StackPanel>
</GroupBox>
</StackPanel>
- Expander
<StackPanel>
<Expander Header="性别" IsExpanded="True" ExpandDirection="Down"
Expanded="Expander_Expanded" Collapsed="Expander_Collapsed">
<StackPanel>
<RadioButton>男</RadioButton>
<RadioButton>女</RadioButton>
</StackPanel>
</Expander>
</StackPanel>
private void Expander_Expanded(object sender, RoutedEventArgs e)
{
MessageBox.Show("Expanded");
}
private void Expander_Collapsed(object sender, RoutedEventArgs e)
{
MessageBox.Show("Collapsed");
}