WPF第四天学习-基本控件
1.RepeatButton:RepeatButton是 ContentControl ,这意味着它可以包含任何类型的单个对象 (例如字符串、图像或面板) 。 有关更多信息,请参见 ContentControl 类。RepeatButton类
表示与类似的控件 Button 。 不过,RepeatButton可让你控制 Click 事件发生的时间和方式。 将 RepeatButton Click 从按下该事件到释放该事件的时间重复引发该事件。Delay属性确定事件
开始的时间。 还可以通过 Interval 属性控制重复的间隔。它和Button的区别就是可以通过一次点击不松鼠标可以实现多次触发Click事件。
关于Delay和Interval:
按下RepeatButton后,在Delay的范围时间内,松掉RepeatButton,触发一次Click事件。
按下RepeatButton后,触发一次Click事件,超过Delay时间,又触发一次Click事件,松掉RepeatButton,停止触发。
按下RepeatButton后,触发一次Click事件,超过Delay时间,又触发一次Click事件,超过Interval,还会触发Click事件,松掉RepeatButton,停止触发。如下图:
实现长按RepeatButton5秒后触发Click事件,且没隔2秒触发一次Ckick事件,直到鼠标松开:
<UniformGrid Rows="3" Columns="2" Grid.Column="1" Grid.Row="0"> <TextBlock Text="1223" Name="txt" Width="100" Height="50"></TextBlock> <RepeatButton Content="456" Click="RepeatButton_Click" Delay="5000" Interval="2000"></RepeatButton> </UniformGrid>
/// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } int value = 0; private void RepeatButton_Click(object sender, RoutedEventArgs e) { value++; this.txt.Text = value.ToString(); } }
2.PassWordBox控件:PassWordBox只有两个属性需要注意,PasWord和PassWordChar
<PasswordBox Password="123584" PasswordChar="/"></PasswordBox>
3.RichTextBox:WPF 中的 RichTextBox 控件允许您查看和编辑文本、段落、图像、表格和其他富文本格式的内容。
RichTextBox 控件承载 RichTextBoxItem 元素的集合。每个项目可以是任何流内容元素。下面的代码片段向 RichTextBox 控件添加了几个项目。
<RichTextBox Margin="0,0,-160,-196"> <FlowDocument> <Paragraph>I am a flow document. Would you like to edit me? <Bold>Go on.</Bold> </Paragraph> <Paragraph Foreground="Blue"> I am student I am student I am student. </Paragraph> </FlowDocument> </RichTextBox>
4.Image控件:加载图片控件
<Image Source="/Images/Logo.png"></Image>
将Images拷贝到项目文件夹下后,右击图片,选择属性--》生成操作,选择资源,这样做是为了将图像文件夹打包到dll文件中,bin目录下将不会有Images文件夹,你也可以访问到该文件夹中的图片资源。
显示图像也可以通过ImageBrush:ImageBrush的优势在于可以通过外在形式控制图像的显示效果
<UniformGrid.Background> <ImageBrush ImageSource="/Images/Logo.png"></ImageBrush> </UniformGrid.Background>
<Ellipse Width="100" Height="100"> <Ellipse.Fill> <ImageBrush ImageSource="/Images/Logo.png"></ImageBrush> </Ellipse.Fill> </Ellipse>
5.RadioButton实现互斥:在同一个容易内的多个RadioButton只能实现单选,若要实现多选,可以使用GroupName
<StackPanel> <RadioButton GroupName="A" Content="男"/> <RadioButton GroupName="A" Content="女"/> <RadioButton GroupName="B" Content="boy"/> <RadioButton GroupName="B" Content="girl"/> </StackPanel>
GroupName想同的是互斥的,上述男女是互斥的。
6.CheckBox:多选控件
<CheckBox IsChecked="{x:Null}" Content="半选中状态"></CheckBox> <CheckBox IsChecked="True" Content="选中状态"></CheckBox> <CheckBox IsChecked="False" Content="未选中状态"></CheckBox>
7.Slider控件:TickFrequency表示每次拖动变化多少个
<Slider Value="10" IsSnapToTickEnabled="True" Minimum="0" Maximum="50" TickFrequency="2" TickPlacement="Both"></Slider>
8.PrograssBar:IsIndeterminate属性表示是否进度条呈现运行的方式。
<ProgressBar IsIndeterminate="True" Background="red" Foreground="Yellow" Width="200" Height="50" Value="50"></ProgressBar>
<ProgressBar IsIndeterminate="True" Orientation="Vertical" Background="Green" Foreground="Yellow" Width="50" Height="200" Value="50"></ProgressBar>
9.ComboBox
<ComboBox Width="200" SelectedIndex="0" SelectionChanged="ComboBox_SelectionChanged"> <ComboBoxItem Content="1"></ComboBoxItem> <ComboBoxItem Content="2"></ComboBoxItem> <ComboBoxItem Content="3"></ComboBoxItem> <ComboBoxItem Content="4"></ComboBoxItem> <ComboBoxItem Content="5"></ComboBoxItem> </ComboBox>
10.ListBox:SelectionMode可以设置多选或者单选
<ListBox SelectionMode="Multiple"> <ListBoxItem Content="AAA"></ListBoxItem> <ListBoxItem Content="BBB"></ListBoxItem> <ListBoxItem Content="CCC"></ListBoxItem> <ListBoxItem Content="DDD"></ListBoxItem> </ListBox>
11.ListView控件:
<ListView Width="200" Background="Yellow"> <ListViewItem Content="444"></ListViewItem> <ListViewItem Content="555"></ListViewItem> <ListViewItem Content="666"></ListViewItem> <ListViewItem Content="666"></ListViewItem> </ListView>
使用ListView控件实现分列:
<ListView Name="lv" Width="400" Background="Yellow"> <ListView.View> <GridView > <GridViewColumn Header="员工Id" Width="100" DisplayMemberBinding="{Binding Id}"></GridViewColumn> <GridViewColumn Header="员工姓名" Width="150" DisplayMemberBinding="{Binding Name}"></GridViewColumn> <GridViewColumn Header="部门" DisplayMemberBinding="{Binding Department}"></GridViewColumn> </GridView> </ListView.View> </ListView>
public MainWindow() { InitializeComponent(); List<Employee> employees = new List<Employee>(); employees.Add(new Employee() { Id=10001,Name="jack",Department="财务部"}); employees.Add(new Employee() { Id=10001,Name="jack",Department="财务部"}); employees.Add(new Employee() { Id=10001,Name="jack",Department="财务部"}); employees.Add(new Employee() { Id=10001,Name="jack",Department="财务部"}); employees.Add(new Employee() { Id=10001,Name="jack",Department="财务部"}); lv.ItemsSource = employees; }
12.DataGrid
<DataGrid Name="dg" Width="500" AutoGenerateColumns="False" CanUserAddRows="False"> <DataGrid.Columns> <DataGridTextColumn Width="100" Header="Id" Binding="{Binding Id}"></DataGridTextColumn> <DataGridTextColumn Width="50" Header="Name" Binding="{Binding Name}"></DataGridTextColumn> <DataGridTextColumn Header="Department" Binding="{Binding Department}"></DataGridTextColumn> <DataGridCheckBoxColumn Header="是否勾选" Binding="{Binding IsSelected}"></DataGridCheckBoxColumn> <DataGridComboBoxColumn x:Name="dbc" Header="性别" SelectedItemBinding="{Binding Gender}"></DataGridComboBoxColumn> <!--数据模版自定义显示--> <DataGridTemplateColumn Header="自定义" Width="100"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Description}"></TextBlock> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>
public MainWindow() { InitializeComponent(); List<Employee> employees = new List<Employee>(); employees.Add(new Employee() {Description="123",Gender="男", IsSelected = true, Id = 10001, Name = "jack", Department = "财务部" }); employees.Add(new Employee() {Description="123",Gender="女", IsSelected = true, Id = 10001, Name = "jack", Department = "财务部" }); employees.Add(new Employee() {Description="123",Gender="女", IsSelected = true, Id = 10001, Name = "jack", Department = "财务部" }); employees.Add(new Employee() {Description="123",Gender="男", IsSelected = false, Id = 10001, Name = "jack", Department = "财务部" }); employees.Add(new Employee() {Description="123",Gender="男", IsSelected = true, Id = 10001, Name = "jack", Department = "财务部" }); //lv.ItemsSource = employees; dg.ItemsSource = employees; dbc.ItemsSource = new string []{"男","女" }; }
13.TabelControl
<TabControl> <TabItem Height="50"> <TabItem.Header> <StackPanel Orientation="Horizontal"> <Image Height="50" Source="/Images/Logo.png"></Image> <TextBlock Text="教育"></TextBlock> </StackPanel> </TabItem.Header> </TabItem> <TabItem Header="BBB"></TabItem> <TabItem Header="CCC"></TabItem> <TabItem Header="DDD"></TabItem> </TabControl>
14.Menu控件:
<Menu> <!--显示字体图标--> <MenuItem Header="文件" Icon="A"> <MenuItem Header="保存"></MenuItem> <!--分隔线分割--> <Separator></Separator> <MenuItem Header="另存为"> <MenuItem.Icon> <Image Source="/Images/Logo.png"></Image> </MenuItem.Icon> </MenuItem> </MenuItem> <MenuItem Header="文件"></MenuItem> <MenuItem Header="文件"></MenuItem> <MenuItem Header="文件"></MenuItem> </Menu> <Border Height="50" Width="200" Background="Yellow"> <Border.ContextMenu> <ContextMenu> <Menu> <!--显示字体图标--> <MenuItem Header="文件" Icon="A"> <MenuItem Header="保存"></MenuItem> <!--分隔线分割--> <Separator></Separator> <MenuItem Header="另存为"> <MenuItem.Icon> <Image Source="/Images/Logo.png"></Image> </MenuItem.Icon> </MenuItem> </MenuItem> <MenuItem Header="文件"></MenuItem> <MenuItem Header="文件"></MenuItem> <MenuItem Header="文件"></MenuItem> </Menu> </ContextMenu> </Border.ContextMenu> </Border>
15.TreeView
<TreeView> <TreeViewItem Header="学校"> <TreeViewItem Header="清华" IsExpanded="True"> <TreeViewItem Header="学生" IsExpanded="True"> <TreeViewItem Header="AAA"></TreeViewItem> <TreeViewItem Header="BBB"></TreeViewItem> <TreeViewItem Header="CCC"></TreeViewItem> </TreeViewItem> </TreeViewItem> <TreeViewItem Header="北大"></TreeViewItem> <TreeViewItem Header="港大"></TreeViewItem> <TreeViewItem Header="东南"></TreeViewItem> </TreeViewItem> </TreeView>
16.Expander控件
<Expander Header="WPF零基础"> <StackPanel> <Label Content="第一课"></Label> <Label Content="第二课"></Label> <Label Content="第三课"></Label> <Label Content="第四课"></Label> <Label Content="第五课"></Label> <Label Content="第流课"></Label> </StackPanel> </Expander>
17.ViewBox控件:ViewBox为界面提供了缩放能力,ViewBox内部的控件会随着界面的变化而自动缩放
<Viewbox> <ListView Height="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" > <ListView.View> <GridView> <GridViewColumn Width="60"> <CheckBox>全选</CheckBox> <GridViewColumn.CellTemplate> <DataTemplate> <CheckBox IsChecked="False" /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Header="设备名称" Width="60"></GridViewColumn> <GridViewColumn Header="型号" Width="60"></GridViewColumn> <GridViewColumn Header="灵敏度" Width="60"></GridViewColumn> <GridViewColumn Width="60"> <CheckBox>采样率</CheckBox> </GridViewColumn> <GridViewColumn Width="60"> <CheckBox>分辨率</CheckBox> </GridViewColumn> <GridViewColumn Width="70"> <CheckBox>输入方式</CheckBox> </GridViewColumn> <GridViewColumn Width="70"> <CheckBox>同步方式</CheckBox> </GridViewColumn> <GridViewColumn Width="70"> <CheckBox>加密方式</CheckBox> </GridViewColumn> <GridViewColumn Width="60"> <CheckBox>秘钥</CheckBox> </GridViewColumn> <GridViewColumn Width="80"> <CheckBox>触发方式</CheckBox> </GridViewColumn> <GridViewColumn Width="70"> <CheckBox>负延迟(s)</CheckBox> </GridViewColumn> <GridViewColumn Width="85"> <CheckBox>采集时长(s)</CheckBox> </GridViewColumn> <GridViewColumn Width="70"> <CheckBox>触发次数</CheckBox> </GridViewColumn> </GridView> </ListView.View> </ListView> </Viewbox>
ViewBox控件内部的元素会根据界面的自动缩放,而不会被遮盖或裁剪:
下面绘制的图形也会随着界面大小的变化而自动缩放。
<Grid> <Viewbox > <Viewbox.RenderTransform> <ScaleTransform ScaleX="0.5" ScaleY="0.5"/> </Viewbox.RenderTransform> <Path Stroke="Black" StrokeThickness="1" Fill="Black" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <Path.Data> <PathGeometry Figures="M 0,0 L 10,0 10,10 Z" /> </Path.Data> </Path> </Viewbox> </Grid>