WPF8-常用控件

打印 上一主题 下一主题

主题 861|帖子 861|积分 2583

控件范例主要控件按钮控件Button、RepeatButton、RadioButton数据体现控件TextBlock、Lable、Image、ItemsControl、ListView、ListBox、DataGrid、DocumentViwer输入控件TextBox、RichTextBox、CheckBox、comboBox、DataPicker、PasswordBox、Slider、ProgressBar菜单导航控件MenuItem、ContextMenu、ToolBar、TreeView、TabControl、Expander信息提示控件Popup、Window、PrintDialog、ToolTip结构控件Grid、StackPanel、WrapPanel、DockPanel、UniformGrid、Canvas、InkCanvas、Border图形控件Line、Rectangle、Ellipse、Polyline、Polygon、Path其他控件ScrollViewer、GroupBox、ViewBox 写在前面:

形而上者谓之道,形而下者谓之器。WPF的运行原理可以谓之“道”,然而,如果没有动手实践写程序这个“器”,何以载道?
本篇较全面的介绍了WPF控件的范例,任何一个WPF控件都不会脱离这几种范例,只要你能举一反三、见微知著,
那么所有控件之间的差别就只在细微之处了。
学习完本篇后你能利用结构元素将控件分列在UI上,能写出有实用意义的GUI程序。
后面的篇幅就是在这个“器”的承载之下,深入研究编写优秀WPF程序的方法了。
1. 按钮控件

1.1. Button 按钮

<Button Content="按钮" Width="100" Height="50" Click="Button_Click"/>
常用属性:
Content: 按钮体现的内容。
Width: 按钮的宽度。
Height: 按钮的高度。
常用事件:
Click: 当用户单击按钮时触发的事件。

1.2. RepeatButton:长按按钮

  1. <RepeatButton Content="RepeatButton" Width="100" Height="50"
  2.               Click="RepeatButton_Click" Interval="500"/>
复制代码

常用属性:
Content: RepeatButton 体现的内容。
Width: RepeatButton 的宽度。
Height: RepeatButton 的高度。
Interval: 重复触发事件之间的时间隔断(以毫秒为单位)。
`
常用事件:
Click: 当用户单击按钮时触发的事件。
在这个示例中,当用户点击按钮时,会触发名为 RepeatButton_Click 的事件处理程序。Interval 属性设置了事件重复触发之间的隔断时间(500 毫秒)。
留意:RepeatButton 是一个特别的按钮,当用户按住它时,会不绝触发 Click 事件,直到按钮被释放。
1.3. RadioButton:单选按钮

  1. <StackPanel>
  2.     <RadioButton Content="RadioButton1" GroupName="Options" Checked="RadioButton_Checked"/>
  3.     <RadioButton Content="RadioButton2" GroupName="Options" Checked="RadioButton_Checked"/>
  4.     <RadioButton Content="RadioButton3" GroupName="Options" Checked="RadioButton_Checked"/>
  5. </StackPanel>
  6. private void RadioButton_Checked(object sender, RoutedEventArgs e)
  7. {
  8.     RadioButton radioButton = (RadioButton)sender;
  9.     MessageBox.Show(radioButton.Content.ToString());
  10. }
复制代码


常用属性:
Content: RadioButton 体现的内容。
GroupName: 组名,确保同一组内的 RadioButton 互斥。
IsChecked: 是否选中。
IsEnabled: 是否启用。
HorizontalAlignment: 水平对齐方式。
VerticalAlignment: 垂直对齐方式。
常用事件:
Checked: 当 RadioButton 被选中时触发的事件。
Unchecked: 当 RadioButton 被取消选中时触发的事件。
2. 数据体现控件

2.1. TextBlock:只读文本控件

<TextBox Width="200" Height="30" Text="Enter text here"/>
常用属性
Text: 设置或获取 TextBlock 中体现的文本内容。
Foreground: 设置或获取 TextBlock 文本的前景色。
Background: 设置或获取 TextBlock 文本的配景色。
FontSize: 设置或获取 TextBlock 文本的字体巨细。
FontFamily: 设置或获取 TextBlock 文本的字体族。
FontWeight: 设置或获取 TextBlock 文本的字体粗细(例如,Normal、Bold)。
FontStyle: 设置或获取 TextBlock 文本的字体样式(例如,Normal、Italic)。
TextWrapping: 设置或获取 TextBlock 文本的换行方式(例如,NoWrap、Wrap、WrapWithOverflow)。
TextAlignment: 设置或获取 TextBlock 文本的对齐方式(例如,Left、Center、Right、Justify)。
TextDecorations: 设置或获取 TextBlock 文本的装饰(例如,下划线、删除线)。
Padding: 设置或获取 TextBlock 的内部间距。
常用事件
TextBlock 本身是一个体现控件,因此它没有输入事件。但是,它继承自 FrameworkElement,因此可以处理一些基本的事件,例如:
MouseLeftButtonDown: 处理鼠标左键按下事件。
MouseLeftButtonUp: 处理鼠标左键抬起事件。
MouseEnter: 处理鼠标进入控件事件。
MouseLeave: 处理鼠标脱离控件事件。
2.2. Lable:标签 体现文本控件

  1. <Label Content="Username:"/>
  2. <TextBox Width="200" Height="30" Margin="0,5"/>
  3. <Label Content="Password:"/>
  4. <PasswordBox Width="200" Height="30" Margin="0,5"/>
  5. <Button Content="Login" Width="100" Height="30" Margin="0,10"/>
复制代码

常用属性
Content: 设置或获取 Label 中体现的文本内容。
Target: 设置或获取 Label 关联的目标控件,用于指示 Label 的用途。
Target用法
  1. <Grid Margin="10">
  2.     <Grid.ColumnDefinitions>
  3.         <ColumnDefinition Width="Auto"/>
  4.         <ColumnDefinition Width="*"/>
  5.     </Grid.ColumnDefinitions>
  6.     <Grid.RowDefinitions>
  7.         <RowDefinition Height="Auto"/>
  8.         <RowDefinition Height="Auto"/>
  9.     </Grid.RowDefinitions>
  10.     <Label Content="_Username:" Target="{Binding ElementName=usernameTextBox}"
  11.       VerticalAlignment="Center"/>
  12.     <TextBox x:Name="usernameTextBox" Grid.Column="1" Width="200"
  13.       Height="30" Margin="5"/>
  14.     <Label Content="_Password:" Target="{Binding ElementName=passwordBox}"
  15.       Grid.Row="1" VerticalAlignment="Center"/>
  16.     <PasswordBox x:Name="passwordBox" Grid.Column="1" Grid.Row="1" Width="200"
  17.       Height="30" Margin="5"/>
  18. </Grid>
复制代码
Label 的 Content 属性包含一个下划线字符 (_) ,体现下一个字符是加速键。例如,_Username: 体现 U 是加速键。
Target 属性绑定到目标控件的名称(利用 {Binding ElementName=…}),当用户按下 Alt 键并激活加速键时,核心会自动移动到目标控件。
x:Name 属性用于给控件命名,以便在 Binding 表达式中引用它们。
按 Alt+U 会将核心移动到用户名输入框(TextBox)。
按 Alt+P 会将核心移动到密码输入框(PasswordBox)。
2.3. ListBox:体现可选择项的列表

  1. <ListBox Width="200" Height="100">
  2.      <ListBoxItem>Item 1</ListBoxItem>
  3.      <ListBoxItem>Item 2</ListBoxItem>
  4.      <ListBoxItem>Item 3</ListBoxItem>
  5. </ListBox>
复制代码
常用属性
ItemsSource: 设置或获取 ListBox 的数据源。
SelectedItem: 获取或设置当前选定的项目。
SelectedIndex: 获取或设置当前选定项目标索引。
SelectionMode: 设置 ListBox 的选择模式。可以是 Single(单选),Multiple(多选),或 Extended(扩展选择)。
ItemTemplate: 定义如何体现 ListBox 中的每个项目。
常用事件
SelectionChanged: 当选择的项目发生更改时触发。
  1. <ListBox x:Name="myListBox" Width="200" Height="100"
  2.             SelectionChanged="ListBox_SelectionChanged">
  3.     <ListBoxItem Content="Item 1"/>
  4.     <ListBoxItem Content="Item 2"/>
  5.     <ListBoxItem Content="Item 3"/>
  6. </ListBox>
复制代码
  1. private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  2. {
  3.     ListBox listBox = sender as ListBox;
  4.     ListBoxItem selectedItem = listBox.SelectedItem as ListBoxItem;
  5.     if (selectedItem != null)
  6.     {
  7.         MessageBox.Show("Selected item: " + selectedItem.Content.ToString());
  8.     }
  9. }
复制代码
  1. <ListBox x:Name="myListBox" Width="200" Height="100"
  2.            SelectionChanged="ListBox_SelectionChanged">
  3.     <ListBox.ItemTemplate>
  4.         <DataTemplate>
  5.             <StackPanel Orientation="Horizontal">
  6.                 <TextBlock Text="{Binding Name}" Width="100"/>
  7.                 <TextBlock Text="{Binding Age}" Width="50"/>
  8.             </StackPanel>
  9.         </DataTemplate>
  10.     </ListBox.ItemTemplate>
  11. </ListBox>
复制代码
  1. public partial class MainWindow : Window
  2. {
  3.     public MainWindow()
  4.     {
  5.         InitializeComponent();
  6.         List<Person> people = new List<Person>
  7.         {
  8.             new Person { Name = "Alice", Age = 30 },
  9.             new Person { Name = "Bob", Age = 25 },
  10.             new Person { Name = "Charlie", Age = 35 }
  11.         };
  12.         myListBox.ItemsSource = people;
  13.     }
  14.     private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  15.     {
  16.         ListBox listBox = sender as ListBox;
  17.         Person selectedPerson = listBox.SelectedItem as Person;
  18.         if (selectedPerson != null)
  19.         {
  20.             MessageBox.Show("Selected person: " + selectedPerson.Name + ", Age: "
  21.                             + selectedPerson.Age);
  22.         }
  23.     }
  24. }
  25. public class Person
  26. {
  27.     public string Name { get; set; }
  28.     public int Age { get; set; }
  29. }
复制代码
ItemsSource: 通过绑定数据源(例如,List)来添补 ListBox。
ItemTemplate: 利用 DataTemplate 定义每个项目标体现方式。
SelectionChanged 事件: 当用户选择项目时,体现所选项目标信息。
2.4. DataGrid:数据表格控件

<DataGrid x:Name="dataGrid" AutoGenerateColumns="True" Margin="10"/>
  1. ……
  2. List<Person> people = new List<Person>
  3.         {
  4.     new Person { Name = "Alice", Age = 30, Email = "alice@example.com" },
  5.     new Person { Name = "Bob", Age = 25, Email = "bob@example.com" },
  6.     new Person { Name = "Charlie", Age = 35, Email = "charlie@example.com" }
  7. };
  8. dataGrid.ItemsSource = people;
  9. ……
  10. public class Person
  11. {
  12.    public string Name { get; set; }
  13.    public int Age { get; set; }
  14.    public string Email { get; set; }
  15. }
复制代码
常用属性
ItemsSource: 设置或获取 DataGrid 的数据源。
AutoGenerateColumns: 设置为 True 时,自动生成列;设置为 False 时,需要手动定义列。
Columns: 定义 DataGrid 中的列集合。
SelectionMode: 设置选择模式,单选或多选。
CanUserAddRows: 设置用户是否可以添加新行。
CanUserDeleteRows: 设置用户是否可以删除行。
CanUserSortColumns: 设置用户是否可以排序列。
常用事件
SelectionChanged: 当选择的项目发生更改时触发。
CellEditEnding: 当单元格编辑即将结束时触发。
RowEditEnding: 当行编辑即将结束时触发。
LoadingRow: 当行加载时触发。
自定义列
  1. <Grid>
  2.     <DataGrid x:Name="dataGrid" AutoGenerateColumns="False" Margin="10"
  3.               SelectionChanged="DataGrid_SelectionChanged">
  4.         <DataGrid.Columns>
  5.             <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="*"/>
  6.             <DataGridTextColumn Header="Age" Binding="{Binding Age}" Width="*"/>
  7.             <DataGridTextColumn Header="Email" Binding="{Binding Email}" Width="*"/>
  8.         </DataGrid.Columns>
  9.     </DataGrid>
  10. </Grid>
复制代码
  1. ……
  2. List<Person> people = new List<Person>
  3.         {
  4.     new Person { Name = "Alice", Age = 30, Email = "alice@example.com" },
  5.     new Person { Name = "Bob", Age = 25, Email = "bob@example.com" },
  6.     new Person { Name = "Charlie", Age = 35, Email = "charlie@example.com" }
  7. };
  8. dataGrid.ItemsSource = people;
  9. ……
  10. private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  11. {
  12.     DataGrid dataGrid = sender as DataGrid;
  13.     Person selectedPerson = dataGrid.SelectedItem as Person;
  14.     if (selectedPerson != null)
  15.     {
  16.         MessageBox.Show("Selected person: " + selectedPerson.Name);
  17.     }
  18. }
  19. ……
  20. public class Person
  21. {
  22.       public string Name { get; set; }
  23.       public int Age { get; set; }
  24.       public string Email { get; set; }
  25. }
复制代码
AutoGenerateColumns: 设置为 False,这样可以自定义列。
DataGridTextColumn: 用于体现文本数据的列,Header 属性设置列标题,Binding 属性绑定到数据源中的相应属性。
SelectionChanged 事件: 当选择的项目发生更改时,体现所选项目标信息。
编辑功能
  1. <DataGrid x:Name="dataGrid" AutoGenerateColumns="False" Margin="10"
  2.                   CanUserAddRows="True" CanUserDeleteRows="True"
  3.                   CanUserSortColumns="True">
  4.     <DataGrid.Columns>
  5.         <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="*"/>
  6.         <DataGridTextColumn Header="Age" Binding="{Binding Age}" Width="*"/>
  7.         <DataGridTextColumn Header="Email" Binding="{Binding Email}" Width="*"/>
  8.     </DataGrid.Columns>
  9. </DataGrid>
复制代码
  1. ……
  2. List<Person> people = new List<Person>
  3.         {
  4.     new Person { Name = "Alice", Age = 30, Email = "alice@example.com" },
  5.     new Person { Name = "Bob", Age = 25, Email = "bob@example.com" },
  6.     new Person { Name = "Charlie", Age = 35, Email = "charlie@example.com" }
  7. };
  8. dataGrid.ItemsSource = people;
  9. ……
  10. public class Person
  11. {
  12.    public string Name { get; set; }
  13.    public int Age { get; set; }
  14.    public string Email { get; set; }
  15. }
复制代码
2.5. ListView:体现列表数据

  1. <ListView x:Name="listView" Width="350" Height="250">
  2.     <ListView.View>
  3.         <GridView>
  4.             <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"
  5.               Width="120"/>
  6.             <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}"
  7.               Width="50"/>
  8.             <GridViewColumn Header="Email" DisplayMemberBinding="{Binding Email}"
  9.               Width="150"/>
  10.         </GridView>
  11.     </ListView.View>
  12. </ListView>
复制代码
  1. List<Person> people = new List<Person>
  2.     {
  3.         new Person { Name = "Alice", Age = 30, Email = "alice@example.com" },
  4.         new Person { Name = "Bob", Age = 25, Email = "bob@example.com" },
  5.         new Person { Name = "Charlie", Age = 35, Email = "charlie@example.com"
  6.     }
  7. };
  8. listView.ItemsSource = people;
复制代码
常用属性
ItemsSource: 设置或获取 ListView 的数据源。
View: 定义 ListView 的体现模式,可以是 GridView、TileView 等。
SelectedItem: 获取或设置当前选定的项目。
SelectedIndex: 获取或设置当前选定项目标索引。
SelectionMode: 设置选择模式,单选或多选。
常用事件
SelectionChanged: 当选择的项目发生更改时触发。
详细视图
  1. <ListView x:Name="listView" Width="350" Height="250" SelectionChanged="ListView_SelectionChanged">
  2.     <ListView.View>
  3.         <GridView>
  4.             <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"
  5.               Width="120"/>
  6.             <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}"
  7.               Width="50"/>
  8.             <GridViewColumn Header="Email" DisplayMemberBinding="{Binding Email}"
  9.               Width="150"/>
  10.         </GridView>
  11.     </ListView.View>
  12. </ListView>
复制代码
  1. public partial class MainWindow : Window
  2. {
  3.     public MainWindow()
  4.     {
  5.         InitializeComponent();
  6.         List<Person> people = new List<Person>
  7.         {
  8.             new Person { Name = "Alice", Age = 30, Email = "alice@example.com" },
  9.             new Person { Name = "Bob", Age = 25, Email = "bob@example.com" },
  10.             new Person { Name = "Charlie", Age = 35, Email = "charlie@example.com" }
  11.         };
  12.         listView.ItemsSource = people;
  13.     }
  14.     private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
  15.     {
  16.         ListView listView = sender as ListView;
  17.         Person selectedPerson = listView.SelectedItem as Person;
  18.         if (selectedPerson != null)
  19.         {
  20.             MessageBox.Show("Selected person: " + selectedPerson.Name);
  21.         }
  22.     }
  23. }
  24. public class Person
  25. {
  26.     public string Name { get; set; }
  27.     public int Age { get; set; }
  28.     public string Email { get; set; }
  29. }
复制代码
自定义项模板
  1. <ListView x:Name="listView" Width="350" Height="250">
  2.     <ListView.ItemTemplate>
  3.         <DataTemplate>
  4.             <StackPanel Orientation="Horizontal">
  5.                 <TextBlock Text="{Binding Name}" Width="120"/>
  6.                 <TextBlock Text="{Binding Age}" Width="50"/>
  7.                 <TextBlock Text="{Binding Email}" Width="150"/>
  8.             </StackPanel>
  9.         </DataTemplate>
  10.     </ListView.ItemTemplate>
  11. </ListView>
复制代码
  1. public partial class MainWindow : Window
  2. {
  3.     public MainWindow()
  4.     {
  5.         InitializeComponent();
  6.         List<Person> people = new List<Person>
  7.         {
  8.             new Person { Name = "Alice", Age = 30, Email = "alice@example.com" },
  9.             new Person { Name = "Bob", Age = 25, Email = "bob@example.com" },
  10.             new Person { Name = "Charlie", Age = 35, Email = "charlie@example.com" }
  11.         };
  12.         listView.ItemsSource = people;
  13.     }
  14. }
  15. public class Person
  16. {
  17.     public string Name { get; set; }
  18.     public int Age { get; set; }
  19.     public string Email { get; set; }
  20. }
复制代码
View: 利用 GridView 来定义详细视图,每个 GridViewColumn 定义一列。
ItemTemplate: 利用 DataTemplate 定义自定义的项结构。
SelectionChanged 事件: 当用户选择项目时,体现所选项目标信息。
2.6. Image:图片

<Image x:Name="myImage" Source="example.jpg" Width="200" Height="150" ImageFailed="Image_ImageFailed" ImageOpened="Image_ImageOpened"/>
常用属性
Source: 设置或获取图像的源路径,可以是相对路径或绝对路径。
Stretch: 指定如何拉伸图像以适应控件的巨细。常用值包括 None、Fill、Uniform、UniformToFill。
StretchDirection: 指定图像可以拉伸的方向。值可以是 UpOnly、DownOnly 或 Both。
Width: 设置图像的宽度。
Height: 设置图像的高度。
  1. private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
  2. {
  3.     MessageBox.Show("Image loading failed: " + e.ErrorException.Message);
  4. }
  5. private void Image_ImageOpened(object sender, RoutedEventArgs e)
  6. {
  7.     MessageBox.Show("Image successfully loaded.");
  8. }
复制代码
Source: 通过 BitmapImage 设置图片源,可以指定图片的路径。
Stretch: 设置图像的拉伸模式,使其在控件中得当体现。
ImageFailed 事件: 当图像加载失败时,体现错误消息。
ImageOpened 事件: 当图像成功加载时,体现成功消息。
2.7. ToggleButton:切换状态按钮控件

  1. <ToggleButton x:Name="toggleButton" Content="Toggle Me" Width="100" Height="50"
  2.                       IsThreeState="True" Checked="ToggleButton_Checked"
  3.                       Unchecked="ToggleButton_Unchecked"
  4.                       Indeterminate="ToggleButton_Indeterminate"/>
复制代码
  1. private void ToggleButton_Checked(object sender, RoutedEventArgs e)
  2. {
  3.     MessageBox.Show("ToggleButton is Checked");
  4. }
  5. private void ToggleButton_Unchecked(object sender, RoutedEventArgs e)
  6. {
  7.     MessageBox.Show("ToggleButton is Unchecked");
  8. }
  9. private void ToggleButton_Indeterminate(object sender, RoutedEventArgs e)
  10. {
  11.     MessageBox.Show("ToggleButton is Indeterminate");
  12. }
复制代码
IsChecked: 利用该属性来确定 ToggleButton 的当前状态。
IsThreeState: 设置为 True 时,ToggleButton 支持三态(选中、未选中、不确定)。
Checked、Unchecked、Indeterminate 事件: 处理 ToggleButton 在差别状态下的事件。
常用属性
IsChecked: 获取或设置 ToggleButton 的状态,true 体现选中,false 体现未选中,null 体现不确定状态。
Content: 设置或获取 ToggleButton 的内容,可以是文本、图像或其他控件。
IsThreeState: 设置或获取 ToggleButton 是否支持三态(true 体现支持三态,false 体现只支持两态)。
常用事件
Checked: 当 ToggleButton 被选中时触发。
Unchecked: 当 ToggleButton 被取消选中时触发。
Indeterminate: 当 ToggleButton 处于不确定状态时触发。
3. 输入控件

3.1. TextBox:输入框

  1. <TextBox  Name="textBox" Width="200" Height="30"
  2.           TextChanged="TextBox_TextChanged" KeyDown="TextBox_KeyDown"
  3.           LostFocus="TextBox_LostFocus"/>
复制代码

常用属性:
Text: 获取或设置 TextBox 中的文本内容。
MaxLength: 指定 TextBox 中答应输入的最大字符数。
IsReadOnly: 指示 TextBox 是否为只读。
AcceptsReturn: 指示 TextBox 是否接受回车键输入(多行文本输入)。
AcceptsTab: 指示 TextBox 是否接受 Tab 键输入。
HorizontalScrollBarVisibility: 设置水平滚动条的可见性(Disabled, Auto, Hidden, Visible)。
VerticalScrollBarVisibility: 设置垂直滚动条的可见性(Disabled, Auto, Hidden, Visible)。
TextWrapping: 指示是否自动换行(NoWrap, Wrap, WrapWithOverflow)。
PlaceholderText: 获取或设置在 TextBox 中未输入文本时体现的占位符文本(如果利用 WPF 早期版本大概需要自定义)。
CaretIndex: 获取或设置插入符号的位置。
常用事件:
TextChanged: 当 TextBox 中的文本内容发生变化时触发的事件。
KeyDown: 当按键按下时触发的事件。
KeyUp: 当按键释放时触发的事件。
LostFocus: 当 TextBox 失去核心时触发的事件。
GotFocus: 当 TextBox 得到核心时触发的事件。
  1. //TextChanged 事件处理程序
  2. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  3. {
  4.     TextBox textBox = sender as TextBox;
  5.     // 处理文本变化事件的代码
  6.     MessageBox.Show("Text changed: " + textBox.Text);
  7. }
  8. //KeyDown 事件处理程序
  9. private void TextBox_KeyDown(object sender, KeyEventArgs e)
  10. {
  11.     if (e.Key == Key.Enter)
  12.     {
  13.         // 处理回车键按下的事件
  14.         MessageBox.Show("Enter key pressed");
  15.     }
  16. }
  17. //LostFocus 事件处理程序
  18. private void TextBox_LostFocus(object sender, RoutedEventArgs e)
  19. {
  20.     TextBox textBox = sender as TextBox;
  21.     // 处理失去焦点事件的代码
  22.     MessageBox.Show("TextBox lost focus");
  23. }
复制代码
3.2. RichTextBox:富文本

  1. <RichTextBox Name="richTextBox" Width="300" Height="200"
  2.              TextChanged="RichTextBox_TextChanged"
  3.              SelectionChanged="RichTextBox_SelectionChanged" />
复制代码
常用属性:
Document: 获取或设置 RichTextBox 体现的 FlowDocument。
IsReadOnly: 指示 RichTextBox 是否为只读。
CaretPosition: 获取或设置插入符号的位置。
Selection: 获取当前选定的内容。
HorizontalScrollBarVisibility: 设置水平滚动条的可见性(Disabled, Auto, Hidden, Visible)。
VerticalScrollBarVisibility: 设置垂直滚动条的可见性(Disabled, Auto, Hidden, Visible)。
AcceptsReturn: 指示是否接受回车键输入。
AcceptsTab: 指示是否接受 Tab 键输入。
常用事件:
TextChanged: 当 RichTextBox 中的文本内容发生变化时触发的事件。
SelectionChanged: 当 RichTextBox 中的文本选择发生变化时触发的事件。
GotFocus: 当 RichTextBox 得到核心时触发的事件。
LostFocus: 当 RichTextBox 失去核心时触发的事件.
TextChanged 事件处理程序
  1. private void RichTextBox_TextChanged(object sender, TextChangedEventArgs e)
  2. {
  3.     RichTextBox richTextBox = sender as RichTextBox;
  4.     // 处理文本变化事件的代码
  5.     MessageBox.Show("Text changed in RichTextBox");
  6. }
  7. SelectionChanged 事件处理程序
  8. private void RichTextBox_SelectionChanged(object sender, RoutedEventArgs e)
  9. {
  10.     RichTextBox richTextBox = sender as RichTextBox;
  11.     // 处理选择变化事件的代码
  12.     MessageBox.Show("Selection changed in RichTextBox");
  13. }
复制代码
利用 XAML 设置初始内容
  1. <RichTextBox Name="richTextBox" Width="300" Height="200"
  2.   TextChanged="RichTextBox_TextChanged"
  3.   SelectionChanged="RichTextBox_SelectionChanged" >
  4.   <RichTextBox.Document>
  5.     <FlowDocument>
  6.       <Paragraph>
  7.         <Run Text="测试" Foreground="Red"></Run>
  8.         <Run Text="开发" Foreground="Blue"></Run>
  9.       </Paragraph>
  10.     </FlowDocument>
  11.   </RichTextBox.Document>
  12. </RichTextBox>
复制代码

利用C#代码设置初始内容
  1. private void initRichTextBox()
  2. {
  3.     Paragraph paragraph = new Paragraph();
  4.     Run run = new Run();
  5.     run.Text = "Hello World";
  6.     run.Foreground = new SolidColorBrush(Colors.Orange);
  7.     paragraph.Inlines.Add(run);
  8.     FlowDocument flowDocument = new FlowDocument(paragraph);
  9.     richTextBox.Document = flowDocument;
  10. }
复制代码

3.3. CheckBox:复选框

  1. <CheckBox Name="checkBox" Content="Check me" IsChecked="False"
  2.           HorizontalAlignment="Center" VerticalAlignment="Center"
  3.           Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
复制代码
常用属性:
Content: CheckBox 体现的内容。
IsChecked: 获取或设置 CheckBox 是否选中(可以是 True, False, 或 null 体现未确定状态)。
IsThreeState: 指示 CheckBox 是否支持三态(选中、未选中、未确定)。
IsEnabled: 指示 CheckBox 是否启用。
HorizontalAlignment: 水平对齐方式。
VerticalAlignment: 垂直对齐方式。
常用事件:
Checked: 当 CheckBox 被选中时触发的事件。
Unchecked: 当 CheckBox 被取消选中时触发的事件。
Indeterminate: 当 CheckBox 进入未确定状态时触发的事件。
  1. //Checked 事件处理程序
  2. private void CheckBox_Checked(object sender, RoutedEventArgs e)
  3. {
  4.     // 处理 CheckBox 选中事件的代码
  5.     MessageBox.Show("CheckBox is checked.");
  6. }
  7. //Unchecked 事件处理程序
  8. private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
  9. {
  10.     // 处理 CheckBox 取消选中事件的代码
  11.     MessageBox.Show("CheckBox is unchecked.");
  12. }
  13. //Indeterminate 事件处理程序(如果 IsThreeState 为 true)
  14. private void CheckBox_Indeterminate(object sender, RoutedEventArgs e)
  15. {
  16.     // 处理 CheckBox 未确定状态事件的代码 半选中状态
  17.     MessageBox.Show("CheckBox is indeterminate.");
  18. }
复制代码
3.4. ComboBox:下拉框

  1. <ComboBox Name="comboBox" Width="200" Height="30"
  2.           SelectionChanged="ComboBox_SelectionChanged">
  3.     <ComboBoxItem Content="Item 1"/>
  4.     <ComboBoxItem Content="Item 2"/>
  5.     <ComboBoxItem Content="Item 3"/>
  6. </ComboBox>
复制代码
常用属性:
ItemsSource: 获取或设置用于添补 ComboBox 的数据源。
SelectedItem: 获取或设置 ComboBox 中当前选定的项。
SelectedIndex: 获取或设置 ComboBox 中当前选定项的索引。
IsEditable: 指示 ComboBox 是否可编辑。
Text: 获取或设置 ComboBox 中当前选定项的文本。
DisplayMemberPath: 获取或设置体现项的属性路径。如果是ItemsSource传值,就体现的字段名
SelectedValuePath: 获取或设置选定项的值路径。
IsDropDownOpen: 获取或设置下拉部分是否已打开。
MaxDropDownHeight: 获取或设置下拉部分的最大高度。
常用事件:
SelectionChanged: 当 ComboBox 中选定项发生变化时触发的事件。
DropDownOpened: 当下拉部分打开时触发的事件。
DropDownClosed: 当下拉部分关闭时触发的事件.
  1. SelectionChanged 事件处理程序
  2. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  3. {
  4.     MessageBox.Show(comboBox.SelectedIndex+"");
  5. }
  6. //数据绑定示例
  7. ....
  8. List<ComboxItem> itemList = new List<ComboxItem>
  9. {
  10.     new ComboxItem { Name = "张三", Id = "1" },
  11.     new ComboxItem { Name = "李四", Id = "2" },
  12.     new ComboxItem { Name = "王二", Id = "3" }
  13. };
  14. comboBox.ItemsSource = itemList;
  15. .....
  16. class ComboxItem
  17. {
  18.      public string Name { get; set; }
  19.      public string Id { get; set; }
  20. }
  21. <ComboBox Name="comboBox" Width="200" Height="30"
  22.            SelectionChanged="ComboBox_SelectionChanged"
  23.            DisplayMemberPath="Name"/>
复制代码
3.5. DataPicker:日期选择器

  1. <DatePicker Name="datePicker" Width="200"
  2.             SelectedDateChanged="DatePicker_SelectedDateChanged" />
复制代码
常用属性:
SelectedDate: 获取或设置 DatePicker 中当前选定的日期。
DisplayDate: 获取或设置在日历中体现的日期。
FirstDayOfWeek: 获取或设置 DatePicker 中每周的第一天。
IsDropDownOpen: 获取或设置下拉部分是否已打开。
DisplayDateStart: 获取或设置日历中可选日期范围的开始日期。
DisplayDateEnd: 获取或设置日历中可选日期范围的结束日期。
IsTodayHighlighted: 获取或设置是否高亮体现今天的日期。
SelectedDateFormat: 获取或设置 DatePicker 体现日期的格式(Short 或 Long)。
常用事件:
SelectedDateChanged: 当 DatePicker 中选定日期发生变化时触发的事件。
CalendarOpened: 当日历部分打开时触发的事件。
CalendarClosed: 当日历部分关闭时触发的事件。
  1. SelectedDateChanged 事件处理程序
  2. private void DatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
  3. {
  4.      MessageBox.Show(datePicker.Text);
  5. }
复制代码
3.6. PasswordBox:密码框

<asswordBox Name="passwordBox" Width="200" PasswordChanged="asswordBox_PasswordChanged"/>
常用属性:
Password: 获取或设置密码框中的密码字符串。
MaxLength: 获取或设置密码框中可以输入的最大字符数。
IsEnabled: 指示 PasswordBox 是否启用。
IsReadOnly: 指示 PasswordBox 是否为只读(通常不常用在 PasswordBox 上)。
HorizontalAlignment: 水平对齐方式。
VerticalAlignment: 垂直对齐方式。
PasswordChar:密码不可见样式
常用事件:
PasswordChanged: 当密码框中的内容发生变化时触发的事件。
PasswordChanged 事件处理程序
  1. private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
  2. {
  3.     MessageBox.Show(passwordBox.Password);
  4. }
复制代码
3.7. Slider:滑块

  1. <Slider Name="slider" Width="200" Height="30"
  2.         Minimum="0" Maximum="100" Value="50"
  3.         TickFrequency="10" IsSnapToTickEnabled="True"
  4.         ValueChanged="Slider_ValueChanged"/>
复制代码
常用属性:
Minimum 和 Maximum:定义了滑块的最小值和最大值,用户只能在这个范围内滑动。
Value:滑块当前的值,用户移动滑块时这个值会更新。
TickFrequency:设置滑块刻度之间的隔断,例如每隔多少单位体现一个刻度。
IsSnapToTickEnabled:如果设置为 True,滑块的值会自动捕获到近来的刻度。
Orientation:可以设置为 Horizontal 或 Vertical,决定滑块是水平照旧垂直。
IsMoveToPointEnabled:如果设置为 True,用户点击滑块轨道的某个位置时滑块会移动到该位置。
TickPlacement:决定刻度线的位置,可以设置为 None、TopLeft、BottomRight 或 Both。
LargeChange 和 SmallChange:定义了滑块在按下 Page Up/Page Down 或方向键时的移动步长。
常用事件:
ValueChanged: 当 Slider 的值发生变化时触发的事件。
  1. //ValueChanged 事件处理程序
  2. private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  3. {
  4.     MessageBox.Show(slider.Value+"");
  5. }
复制代码
3.8. ProgressBar:进度条

  1. <ProgressBar Name="progressBar" Width="200" Height="30" Minimum="0"
  2.              Maximum="100" Value="50"
  3.              ValueChanged="progressBar_ValueChanged"/>
复制代码

常用属性:
Minimum 和 Maximum:定义了进度条的最小值和最大值,用户只能在这个范围内设置值。
Value:进度条当前的值,可以程序matically 更新以体现进度。
IsIndeterminate:如果设置为 True,进度条进入不确定模式,体现正在举行无法预估时间的操作。来回流动
Orientation:可以设置为 Horizontal 或 Vertical,决定进度条的方向。
Foreground:设置进度条的前景色,即体现进度的颜色。
ValueChanged:当进度条的值发生变化时触发,可以用于及时更新 UI 或举行其他操作。
常用事件:
ValueChanged: 当 ProgressBar 的值发生变化时触发的事件。
  1. //ValueChanged 事件处理程序
  2. private void progressBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  3. {
  4.      MessageBox.Show(progressBar.Value+"");
  5. }
复制代码
4. 菜单导航控件

4.1. MenuItem:菜单

  1. <Menu>
  2.     <MenuItem Header="_File">
  3.         <MenuItem Header="_New" Click="NewMenuItem_Click"/>
  4.         <MenuItem Header="_Open" Click="OpenMenuItem_Click"/>
  5.         <MenuItem Header="_Save" Click="SaveMenuItem_Click"/>
  6.         <Separator/>
  7.         <MenuItem Header="_Exit" Click="ExitMenuItem_Click"/>
  8.     </MenuItem>
  9.     <MenuItem Header="_Edit">
  10.         <MenuItem Header="_Cut" Click="CutMenuItem_Click"/>
  11.         <MenuItem Header="_Copy" Click="CopyMenuItem_Click"/>
  12.         <MenuItem Header="_Paste" Click="PasteMenuItem_Click"/>
  13.     </MenuItem>
  14. </Menu>
复制代码
常用属性:
Header: 获取或设置体现在 MenuItem 上的文本。第一个加入下划线"_",体现alt + 快捷方式打开
Icon: 获取或设置体现在 MenuItem 左侧的图标。
IsCheckable: 获取或设置 MenuItem 是否可以被选中或取消选中。
IsChecked: 获取或设置 MenuItem 是否被选中。
InputGestureText: 获取或设置体现在 MenuItem 右侧的快捷键文本。仅体现提示作用
Items: 获取此 MenuItem 的子菜单项集合。
StaysOpenOnClick: 指示点击 MenuItem 后是否保持菜单打开。
Separator:在菜单中增长一个隔行线
常用事件:
Click: 当 MenuItem 被点击时触发的事件。
Checked: 当 MenuItem 被选中时触发的事件(仅当 IsCheckable 为 True 时适用)。
Unchecked: 当 MenuItem 被取消选中时触发的事件(仅当 IsCheckable 为 True 时适用)。
事件处理程序
  1. private void NewMenuItem_Click(object sender, RoutedEventArgs e)
  2. {
  3.     MessageBox.Show("New");
  4. }
  5. private void OpenMenuItem_Click(object sender, RoutedEventArgs e)
  6. {
  7.     MessageBox.Show("Open");
  8. }
  9. private void SaveMenuItem_Click(object sender, RoutedEventArgs e)
  10. {
  11.     MessageBox.Show("New");
  12. }
  13. private void ExitMenuItem_Click(object sender, RoutedEventArgs e)
  14. {
  15.     MessageBox.Show("Exit");
  16. }
  17. private void CutMenuItem_Click(object sender, RoutedEventArgs e)
  18. {
  19.     MessageBox.Show("Cut");
  20. }
  21. private void CopyMenuItem_Click(object sender, RoutedEventArgs e)
  22. {
  23.     MessageBox.Show("Copy");
  24. }
  25. private void PasteMenuItem_Click(object sender, RoutedEventArgs e)
  26. {
  27.     MessageBox.Show("Paste");
  28. }
复制代码
4.2. ContextMenu:上下文菜单

  1. <TextBox x:Name="textBox" Width="200" Height="30" VerticalAlignment="Top" HorizontalAlignment="Center">
  2.     <TextBox.ContextMenu>
  3.         <ContextMenu Opened="ContextMenu_Opened">
  4.             <MenuItem Header="Cut" Click="CutMenuItem_Click"/>
  5.             <MenuItem Header="Copy" Click="CopyMenuItem_Click"/>
  6.             <MenuItem Header="Paste" Click="PasteMenuItem_Click"/>
  7.         </ContextMenu>
  8.     </TextBox.ContextMenu>
  9. </TextBox>
  10. <Button x:Name="button" Content="按钮" Width="200" Height="30"
  11.   VerticalAlignment="Top" HorizontalAlignment="Center">
  12.     <Button.ContextMenu>
  13.         <ContextMenu Closed="ContextMenu_Closed">
  14.             <MenuItem Header="Cut" Click="CutMenuItem_Click"/>
  15.             <MenuItem Header="Copy" Click="CopyMenuItem_Click"/>
  16.             <MenuItem Header="Paste" Click="PasteMenuItem_Click"/>
  17.         </ContextMenu>
  18.     </Button.ContextMenu>
  19. </Button>
复制代码
事件处理程序
  1. private void CutMenuItem_Click(object sender, RoutedEventArgs e)
  2. {
  3.     MessageBox.Show("Cut");
  4. }
  5. private void CopyMenuItem_Click(object sender, RoutedEventArgs e)
  6. {
  7.     MessageBox.Show("Copy");
  8. }
  9. private void PasteMenuItem_Click(object sender, RoutedEventArgs e)
  10. {
  11.     MessageBox.Show("Paste");
  12. }
  13. private void ContextMenu_Opened(object sender, RoutedEventArgs e)
  14. {
  15.     MessageBox.Show("Open");
  16. }
  17. private void ContextMenu_Closed(object sender, RoutedEventArgs e)
  18. {
  19.     MessageBox.Show("Close");
  20. }
复制代码
常用属性
Items: 获取此 ContextMenu 的子菜单项集合。
IsOpen: 获取或设置 ContextMenu 是否可见。
PlacementTarget: 获取或设置 ContextMenu 的放置目标对象。
Placement: 获取或设置 ContextMenu 的放置位置。
HorizontalOffset: 获取或设置 ContextMenu 相对于 PlacementTarget 水平方向的偏移量。
VerticalOffset: 获取或设置 ContextMenu 相对于 PlacementTarget 垂直方向的偏移量.
常用事件
Opened 和 Closed 事件处理程序在 ContextMenu 打开和关闭时体现消息框。
4.3. ToolBar:创建工具栏

  1. <DockPanel>
  2.     <!-- ToolBarTray 用于容纳多个 ToolBar -->
  3.     <ToolBarTray DockPanel.Dock="Top">
  4.         <!-- 定义一个 ToolBar -->
  5.         <ToolBar>
  6.             <Button Content="New" Click="NewButton_Click">
  7.                 <Button.ToolTip>
  8.                     <ToolTip Content="Create a new file"/>
  9.                 </Button.ToolTip>
  10.             </Button>
  11.             <Button Content="Open" Click="OpenButton_Click">
  12.                 <Button.ToolTip>
  13.                     <ToolTip Content="Open an existing file"/>
  14.                 </Button.ToolTip>
  15.             </Button>
  16.             <Button Content="Save" Click="SaveButton_Click">
  17.                 <Button.ToolTip>
  18.                     <ToolTip Content="Save the current file"/>
  19.                 </Button.ToolTip>
  20.             </Button>
  21.             <Separator/>
  22.             <ToggleButton Content="Bold" Click="BoldButton_Click">
  23.                 <ToggleButton.ToolTip>
  24.                     <ToolTip Content="Toggle bold text"/>
  25.                 </ToggleButton.ToolTip>
  26.             </ToggleButton>
  27.             <Separator/>
  28.             <ComboBox Width="100" SelectionChanged="FontComboBox_SelectionChanged">
  29.                 <ComboBoxItem Content="Arial"/>
  30.                 <ComboBoxItem Content="Calibri"/>
  31.                 <ComboBoxItem Content="Times New Roman"/>
  32.             </ComboBox>
  33.         </ToolBar>
  34.     </ToolBarTray>
  35.     <!-- 主内容区域 -->
  36.     <TextBox Name="textBox" Margin="10" VerticalScrollBarVisibility="Auto"
  37.       AcceptsReturn="True"/>
  38. </DockPanel>
复制代码
常用属性
Band: 获取或设置工具栏所在的带的索引(仅用于 ToolBarTray)。
BandIndex: 获取或设置工具栏在带中的位置索引。
Orientation: 获取或设置工具栏的结构方向(水平或垂直)。
ToolBarOverflowPanel: 获取溢出头板,用于在工具栏空间不敷时存放多余的控件。
利用 Band 和 BandIndex 属性来控制 ToolBar 在 ToolBarTray 中的位置。
常用事件
Loaded 和 Unloaded 事件处理程序在 ToolBar 加载和卸载时体现消息框。
  1. private void NewButton_Click(object sender, RoutedEventArgs e)
  2. {
  3.     textBox.Clear();
  4. }
  5. private void OpenButton_Click(object sender, RoutedEventArgs e)
  6. {
  7.     MessageBox.Show("Open");
  8. }
  9. private void SaveButton_Click(object sender, RoutedEventArgs e)
  10. {
  11.     MessageBox.Show("Save");
  12. }
  13. private void BoldButton_Click(object sender, RoutedEventArgs e)
  14. {
  15.     textBox.FontWeight = FontWeights.Bold;
  16. }
  17. private void FontComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  18. {
  19.     ComboBox? comboBox = sender as ComboBox;
  20.     this.textBox.Text += $"\r\n{comboBox.SelectedItem}";
  21. }
复制代码
4.4. TreeView:树状视图

  1. <TreeView x:Name="treeView" SelectedItemChanged="treeView_SelectedItemChanged">
  2.     <TreeViewItem Header="Root">
  3.         <TreeViewItem Header="C-01">
  4.             <TreeViewItem Header="C-01-01" />
  5.             <TreeViewItem Header="C-01-02" />
  6.         </TreeViewItem>
  7.         <TreeViewItem Header="C-02" />
  8.         <TreeViewItem Header="C-03" />
  9.     </TreeViewItem>
  10. </TreeView>
复制代码
常用属性
Items: 获取此 TreeViewItem 的子节点集合。
IsExpanded: 获取或设置是否睁开该 TreeViewItem。
Header: 获取或设置体现在 TreeViewItem 上的文本或内容。
IsSelected: 获取或设置是否选择该 TreeViewItem。
Tag: 获取或设置与该 TreeViewItem 关联的对象。
常用事件
SelectedItemChanged 事件处理程序在选择的项发生变化时体现消息框,体现当前选择的节点的标题。
Collapsed: 当 TreeViewItem 折叠时触发的事件。
Expanded: 当 TreeViewItem 睁开时触发的事件。
SelectedItemChanged 事件处理程序
  1. private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
  2. {
  3.     MessageBox.Show(((TreeViewItem)treeView.SelectedItem).Header as string);
  4. }
复制代码
4.5. TabControl:选项卡

  1. <TabControl x:Name="tabControl" SelectionChanged="TabControl_SelectionChanged">
  2.     <TabItem Header="Tab01">
  3.         <TextBlock Text="01"/>
  4.     </TabItem>
  5.     <TabItem Header="Tab02">
  6.         <TextBlock Text="02"/>
  7.     </TabItem>
  8. </TabControl>
  9. <TextBlock x:Name="tb" Height="30" Width="100" />
复制代码
常用属性
Items: 获取此 TabControl 的选项卡集合。
SelectedIndex: 获取或设置当前选中的选项卡的索引。
SelectedItem: 获取或设置当前选中的选项卡。
TabStripPlacement: 获取或设置选项卡的位置(顶部、底部、左侧或右侧)。
TabStripPlacement: 获取或设置选项卡标题的对齐方式。
常用事件
SelectionChanged 事件处理程序在选择的选项卡发生变化时体现消息框,体现当前选择的选项卡的标题。
PreviewMouseDown: 当用户点击选项卡标题时触发的事件。
PreviewMouseMove: 当用户移动鼠标到选项卡标题上时触发的事件。
SelectionChanged事件处理程序
  1. private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
  2. {
  3.     TabItem tabItem = (tabControl.SelectedItem) as TabItem;
  4.     tb.Text = tabItem.Header.ToString();
  5. }
复制代码
4.6. Expander:折叠控件

  1. <Expander Header="Details" IsExpanded="True" Expanded="Expander_Expanded"
  2.   Collapsed="Expander_Collapsed">
  3.     <TextBlock Text="Expander" />
  4. </Expander>
复制代码

常用属性
Header: 获取或设置 Expander 控件的标题。
IsExpanded: 获取或设置一个值,指示 Expander 控件当前是否处于睁开状态。
Content: 获取或设置 Expander 控件的内容。
常用事件
Expanded 事件处理程序在 Expander 控件睁开时体现消息框。
Collapsed 事件处理程序在 Expander 控件折叠时体现消息框。
  1. private void Expander_Expanded(object sender, RoutedEventArgs e)
  2. {
  3.     MessageBox.Show("展开");
  4. }
  5. private void Expander_Collapsed(object sender, RoutedEventArgs e)
  6. {
  7.     MessageBox.Show("关闭");
  8. }
复制代码
5. 信息提示控件

5.1. Popup:弹出式窗口

  1. <Button Content="按钮" Name="btn" Click="Button_Click_2"/>
  2. <Popup x:Name="popup" PlacementTarget="{Binding ElementName=btn}" Placement="Bottom">
  3.     <TextBlock Text="textBlock" Background="AliceBlue" />
  4. </Popup>
复制代码
常用属性
IsOpen: 获取或设置一个值,指示 Popup 是否处于打开状态。
Placement: 获取或设置 Popup 的位置相对于其放置目标的方式,可以是相对于左上角、右上角、底部等。
PlacementTarget: 获取或设置 Popup 的放置目标,即触发 Popup 体现的控件。
Child: 获取或设置 Popup 内容的根元素。
常用事件
Opened: 当 Popup 打开时发生的事件。
Closed: 当 Popup 关闭时发生的事件。
  1. private void Button_Click_2(object sender, RoutedEventArgs e)
  2. {
  3.     popup.IsOpen = !popup.IsOpen;
  4. }
复制代码
5.2. Window:窗口

  1. <Window x:Class="WpfApp.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         Title="MainWindow" Height="350" Width="525">
  5.     <Grid>
  6.         <!-- 在此处添加其他 UI 元素 -->
  7.     </Grid>
  8. </Window>
复制代码
常用属性
Title: 获取或设置窗口的标题。
Height 和 Width: 获取或设置窗口的高度和宽度。
MinHeight 和 MinWidth: 获取或设置窗口的最小高度和最小宽度。
MaxHeight 和 MaxWidth: 获取或设置窗口的最大高度和最大宽度。
WindowStyle: 获取或设置窗口的样式,如无边框窗口、标准窗口等。
ResizeMode: 获取或设置窗口的调解巨细模式,如答应调解巨细、禁止调解巨细等。
常用事件
Loaded: 当窗口加载完成时触发的事件。
Closing: 当窗口即将关闭时触发的事件。
Closed: 当窗口已经关闭时触发的事件。
5.3. PrintDialog:打印窗口

<Button Content="rint" Click="Button_Click"/>
常用属性和方法
PrintQueue: 获取或设置要利用的打印队列(打印机)。
PrintTicket: 获取或设置用于打印作业的打印选项。
PrintableAreaHeight 和 PrintableAreaWidth: 获取当前打印机的可打印区域的高度和宽度。
ShowDialog(): 体现打印对话框,答应用户选择打印机和设置打印选项。
在这个示例中,当用户点击按钮时,将创建一个 PrintDialog 对象并体现对话框。用户可以在对话框中选择打印机、设置打印选项,并确认打印操作。一旦用户点击了对话框中的打印按钮,并且打印操作成功开始,ShowDialog() 方法将返回 true,您可以在此处执行实际的打印操作。
  1. private void Button_Click(object sender, RoutedEventArgs e)
  2. {
  3.     PrintDialog printDialog = new PrintDialog();
  4.     if (printDialog.ShowDialog() == true)
  5.     {
  6.         // 在这里执行打印操作
  7.     }
  8. }
复制代码
5.4. ToolTip:悬停时提示按钮

<Button Content="按钮" ToolTip="button!" />
解释
在这个示例中,当用户将鼠标悬停在按钮上时,将体现一个简单的提示框,其中包含文本 “button!”。
常用属性
Content: 获取或设置 ToolTip 的内容,可以是任何 WPF 控件或元素。
Placement: 获取或设置 ToolTip 的位置相对于其关联的控件的方式,可以是相对于鼠标、控件的左上角、右上角等。
PlacementTarget: 获取或设置 ToolTip 的放置目标,即与其关联的控件。
常用事件
Opened: 当 ToolTip 打开时发生的事件。
Closed: 当 ToolTip 关闭时发生的事件。
6. 结构控件

在我的另一篇博客中有详细介绍《wpf基础结构》
7. 图形控件

7.1. Line:线

<Line X1="10" Y1="10" X2="100" Y2="100" Stroke="AliceBlue" StrokeThickness="2"/>

常用属性
X1: 获取或设置直线的起点的 x 坐标。
Y1: 获取或设置直线的起点的 y 坐标。
X2: 获取或设置直线的终点的 x 坐标。
Y2: 获取或设置直线的终点的 y 坐标。
Stroke: 获取或设置直线的颜色。
StrokeThickness: 获取或设置直线的粗细。
优点
简单易用:Line 控件提供了一种简单的方式来在界面上绘制直线。
灵活性:您可以根据需要设置直线的起点、终点、颜色和粗细,从而实现差别样式的直线。
7.2. Rectangle:矩形

<Rectangle Width="100" Height="50" Fill="LightBlue" Stroke="Black" StrokeThickness="2"/>

常用属性
Width: 获取或设置矩形的宽度。
Height: 获取或设置矩形的高度。
Fill: 获取或设置矩形的添补颜色。
Stroke: 获取或设置矩形的边框颜色。
StrokeThickness: 获取或设置矩形的边框粗细。
7.3. Ellipse:圆

<Ellipse Width="100" Height="50" Fill="LightGreen" Stroke="Black" StrokeThickness="2"/>

常用属性
Width: 获取或设置椭圆形的宽度。
Height: 获取或设置椭圆形的高度。
Fill: 获取或设置椭圆形的添补颜色。
Stroke: 获取或设置椭圆形的边框颜色。
StrokeThickness: 获取或设置椭圆形的边框粗细。
7.4. Polyline:点的集合

  <olyline Points="10,10 50,50 100,20" Stroke="Black" StrokeThickness="2"/>

常用属性
Points: 获取或设置折线上各个极点的坐标。
Stroke: 获取或设置折线的颜色。
StrokeThickness: 获取或设置折线的粗细
7.5. Polygon:点的集合 封闭

<olygon Points="10,10 50,50 100,20" Fill="LightBlue" Stroke="Black" StrokeThickness="2"/>

常用属性
Points: 获取或设置多边形上各个极点的坐标。
Fill: 获取或设置多边形的添补颜色。
Stroke: 获取或设置多边形的边框颜色。
StrokeThickness: 获取或设置多边形的边框粗细。
留意:
Polyline: 不是封闭的,因为末了一条线段不连接到第一条线段,因此不形成封闭的图形。
Polygon: 是封闭的,因为末了一条线段连接到第一条线段,形成封闭的图形。
7.6. Path:path图形

  1. <Path Stroke="Black" StrokeThickness="2">
  2.     <Path.Data>
  3.         <PathGeometry>
  4.             <PathGeometry.Figures>
  5.                 <PathFigureCollection>
  6.                     <PathFigure StartPoint="10,10">
  7.                         <LineSegment Point="50,50"/>
  8.                         <LineSegment Point="100,20"/>
  9.                         <LineSegment Point="10,10"/>
  10.                     </PathFigure>
  11.                 </PathFigureCollection>
  12.             </PathGeometry.Figures>
  13.         </PathGeometry>
  14.     </Path.Data>
  15. </Path>
复制代码

常用属性
Stroke: 获取或设置路径的边框颜色。
StrokeThickness: 获取或设置路径的边框粗细。
Fill: 获取或设置路径的添补颜色。
Data: 获取或设置形貌路径形状的几何数据。
7.7. Path微语言:

  1. <Path Stroke="Black" StrokeThickness="2" Fill="LightBlue"
  2.   Data="M 10,10
  3.         L 50,50
  4.         Q 100,20 150,50
  5.         A 50,30 45 1 1 200,100
  6.         C 250,50 300,150 350,100
  7.         Z"/>
复制代码

M (move to): 移动到指定的坐标 (x,y)。
L (line to): 绘制一条从当前位置到指定坐标 (x,y) 的直线。
Q (quadratic Bezier curve to): 绘制一条二次贝塞尔曲线,指定控制点和终点。
A (arc to): 绘制一个椭圆弧,指定椭圆的半径、旋转角度、弧度和方向。
C (cubic Bezier curve to): 绘制一条三次贝塞尔曲线,指定两个控制点和终点。
Z (close path): 关闭路径,绘制一条直线回到路径的起点。
8. 其他控件

8.1. ScrollViewer:滚动容器

  1. <ScrollViewer HorizontalScrollBarVisibility="Visible"
  2.               VerticalScrollBarVisibility="Visible"
  3.               ScrollChanged="ScrollViewer_ScrollChanged">
  4.     <StackPanel>
  5.         <TextBlock Text="Item 1" FontSize="16"/>
  6.         <TextBlock Text="Item 2" FontSize="16"/>
  7.         <TextBlock Text="Item 3" FontSize="16"/>
  8.         <TextBlock Text="Item 4" FontSize="16"/>
  9.         <TextBlock Text="Item 5" FontSize="16"/>
  10.         <TextBlock Text="Item 6" FontSize="16"/>
  11.         <TextBlock Text="Item 7" FontSize="16"/>
  12.         <TextBlock Text="Item 8" FontSize="16"/>
  13.         <TextBlock Text="Item 9" FontSize="16"/>
  14.         .....
  15.         <TextBlock Text="Item 10" FontSize="16"/>
  16.     </StackPanel>
  17. </ScrollViewer>
复制代码
常用属性
HorizontalScrollBarVisibility: 获取或设置水平滚动条的可见性。可选值有 Disabled, Auto, Hidden, 和 Visible。
VerticalScrollBarVisibility: 获取或设置垂直滚动条的可见性。可选值有 Disabled, Auto, Hidden, 和 Visible。
Content: 获取或设置 ScrollViewer 的子控件。
常用事件
ScrollChanged: 当 ScrollViewer 的滚动位置发生变化时发生。
优点
ScrollViewer 控件提供了简单的滚动支持,可以轻松处理内容超出可视区域的情况。
通过设置滚动条的可见性,可以根据需要自动体现或隐蔽滚动条。
  1. private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
  2. {
  3.     MessageBox.Show("ScrollViewer scrolled!");
  4. }
复制代码
8.2. GroupBox:分组容器

  1. <StackPanel>
  2.     <GroupBox Header="User Information" Margin="10">
  3.         <StackPanel>
  4.             <TextBlock Text="Name:" Margin="5"/>
  5.             <TextBox Width="200" Margin="5"/>
  6.             <TextBlock Text="Age:" Margin="5"/>
  7.             <TextBox Width="50" Margin="5"/>
  8.         </StackPanel>
  9.     </GroupBox>
  10.     <GroupBox Header="Address" Margin="10">
  11.         <StackPanel>
  12.             <TextBlock Text="Street:" Margin="5"/>
  13.             <TextBox Width="200" Margin="5"/>
  14.             <TextBlock Text="City:" Margin="5"/>
  15.             <TextBox Width="150" Margin="5"/>
  16.             <TextBlock Text="Country:" Margin="5"/>
  17.             <TextBox Width="150" Margin="5"/>
  18.         </StackPanel>
  19.     </GroupBox>
  20. </StackPanel>
复制代码

常用属性
Header: 获取或设置 GroupBox 的标题。
HeaderTemplate: 获取或设置一个数据模板,用于自定义 GroupBox 标题的外观。
Content: 获取或设置 GroupBox 内的内容。
8.3. ViewBox:体现盒子

  1. <Viewbox Stretch="Uniform">
  2.     <StackPanel>
  3.         <TextBlock Text="Hello, ViewBox!" FontSize="20"/>
  4.         <Button Content="Click Me" Width="100" Height="30"/>
  5.     </StackPanel>
  6. </Viewbox>
复制代码

常用属性
Child: 获取或设置 ViewBox 的单个子元素。
Stretch: 获取或设置如何拉伸和缩放子元素以适应 ViewBox 的巨细。大概的值包括:
Stretch=“None”:矩形不被拉伸。
Stretch=“Fill”:矩形被拉伸以完全添补 ViewBox,不保持宽高比。
Stretch=“Uniform”:矩形等比例缩放以适应 ViewBox,保持宽高比。
Stretch=“UniformToFill”:矩形等比例缩放以完全添补 ViewBox,保持宽高比,但大概会被剪裁。
StretchDirection: 获取或设置 ViewBox 如何限制内容的缩放。大概的值包括:
UpOnly: 只答应放大内容。
DownOnly: 只答应缩小内容。
Both: 答应放大和缩小内容。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

没腿的鸟

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表