WPF控件学习笔记···

WPF控件

1内容控件:

ContentControl直接从Control类中派生出来,内容控件的最大特征是含有一个Content属性,Content属性的类型是Object,因此内容控件可以是.Net的任何类。若Content属性是一个没有用户界面的类,WPF会调用Object类的ToString方法,从而在控件中显示字符串,如果Content是UI元素,WPF会调用OnRender方法,从而在控件中绘制出该UI元素。

框架控件(Frame)

框架控件的主要作用是将框架中的内容与界面上的其他部分分开,可含有任何东西,但只能有一个直接的子控件,Frame可以作为其他控件的子控件但是其宿主包容器的相关属性不会传递到Frame中,起到了隔离作用。Frame控件的最主要的作用是要在其中嵌入HTML内容,方法是设定Source属性。

WPF按钮(Button)

WPF中的按钮都是从ButtonBase抽象类中派生出来的,ButtonBase的四个属性    ClickMode、 IsFocused、IsMouseOver、IsPressed。

拨动按钮(ToggleButton)

ToggleButton的IsChecked属性有三个值:True、False、Null。分别对应三个事件:Checked、UnChecked、Indeterminate,一般不直接创建ToggleButton的实例而是使用其派生类:CheckBox和RadioButtonCheckBox控件没有增加任何属性,它为ToggleButton提供外观。

RadioButton:若在一组选项里面,你必须而且只能选择一个选项,需要使用RadioButton。Radiobutton增加了一个属性GroupName,类型为string

 重复按钮(RepeatButton)

重复按钮可以在鼠标按下左键时,不停地发出单击Click事件,它增加了两个属性:Delay和Interval。Delay设置从按下左键到发送Click事件的延迟时间,单位是ms。Interval设置的是两个单击事件的间隔时间,单位也是ms。

带有标题栏的内容控件(HeaderedContentControl)

从HeaderedContentControl中派生出三个类:GroupBox、Expander、TabItem。TabItem控件和TabControl一起使用。带有标题的内容控件除了具有内容控件的Content属性还具有一个新的标题属性Header。Header属性和Content属性的类型都是Object。换句话说,任何.NET对象都可以成为标题Header。当然最常用的标题是字符串,但也可以是其他任何控件或控件组合。

Expander控件,压缩时只显示标题,展开时才显示内容。GroupBox是把一些意义相近的控件放在一起,同时用一个带标题的边框把他们组合起来。

标签控件(Label)

标签控件也是一个常用的内容控件,我们使用标签的最主要的功能是显示字符串,Text Block和TextBox也可以显示字符串,标签控件区别于他们的功能有两点:1.在默认的情况下标签控件是只读的,颜色是灰色的;2.标签控件内置了一个AccessText控件,可以直接设定热键到目标控件。

ToolTip

ToolTip属性是定义在FrameworkElement类和FrameworkContent中的。这意味着不仅WPF中的所有控件都可以使用ToolTip,而且图形和文档也可以有ToolTip,其次ToolTip和其他WPF控件一样,其中可以含有其他任何WPF控件及其组合。使用ToolTip虽然是一个控件,但其本身不具备接受用户输入的功能;ToolTip虽然是一个控件,但它不能像其他控件那样作为其他控件的子控件,它必须和某个控件的ToolTip属性相连。鼠标离开控件时,其ToolTip会自动隐藏,有时候,我们希望控制ToolTip的显示时间,ToolTipService类就是为此而生的。该类含有控制ToolTip水平或竖直方向位移的属性HorizontalOffset、VerticalOffset;控制ToolTip显示时间的ShowDuration属性;控制是否在未使能的控件上显示ToolTip的ShowOnDisabled属性等。

ScrollViewer

在WPF中显示滚动条要用到ScrollViewer类,这是内容控件,故只能含有一个子控件。

2.条目控件 

 ItemsControl直接派生自Control类,与内容控件不同,ItemsControl中含有Items属性,这个属性具有ItemCollection类型。其内容可以是任何一个Object类型的对象,WPF在显示Items属性中的对象时,如果这个对象是UI元素,就直接条用UI元素的OnRender方法;若不是UI元素,WPF会创建一TextBlock,并在TextBox中显示该对象的ToString方法所返回的结果。ItemsControl类中还有一个重要的属性IteSource属性,这是用来做数据绑定的。当我们使用了ItemsSource属性,Items属性就会自动设置为null。

菜单(Menu)

每个菜单中有菜单条目(MenuItem),MenuItem是个带有标题的条目控件,派生自HeaderedItemControl类,MenuItem常用属性:Icon,InputGestureText(用来显示热键,仅仅是显示功能),IsCheckable,IsChecked,IsPressed,Command。菜单有两种用法,一种是下拉式菜单,另一种是弹出式菜单。

 工具条(ToolBar)

WPF工具条涉及两个类:ToolBarTray和ToolBar。ToolBarTray含有一个或多个ToolBar,ToolBar中含有一个或多个WPF控件,这里的控件通常是Button、
CheckBox、ListBox、ComboBox、和TextBox等。ToolBarTray负责工具条的排版,Background属性设置工具条的背景色,Orientation设置工具条的方向水平和竖直。当ToolBarTray中有两个或以上工具条时,需要设置工具条Toolbar的Band属性,工具条还有属性HasOverFlowItems和IsOverFlowOpen,OverflowMode

Selector

该类是ComboBox、ListBox、TabControl的基类,这三个控件的共同特点是:其中包含一个或者多个条目供用户选择,Selector是一个抽象类,不可以直接创建Selector对象,Selector中的常用属性有:SelectedIndex、SelectedItem、SelectedValue、SelectedPath。

组合框(ComboBox)主要由字符框(TextBlock)、拨动按钮(ToggleButton)和Popup几个控件组成,有时需要组合框支持用户在输入字符串时自动选择组合框中的条目的特性,用户输入的字符串可以不在组合框的条目中;有时候有需要限制用户输入字符串。当需要选择合适的组合框特性时,要设置IsEditable和IsReadOnly两个属性。

View Code
<Window x:Class="MyFirstWpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        x:Name="Window"
        Title="加油" Height="200" Width="500" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0" Grid.Row="0" Orientation="Horizontal" Margin="5">
            <Label Height="30" >IsEditable</Label>
            <ComboBox Name="cIsEditable" SelectedIndex="0" SelectionChanged="OnEditableChanged" Height="30">
                <ComboBoxItem>True</ComboBoxItem>
                <ComboBoxItem>False</ComboBoxItem>
            </ComboBox>
        </StackPanel>
        <StackPanel Grid.Column="1" Grid.Row="0" Orientation="Horizontal" Margin="5">
            <Label Height="30">IsReadOnly:</Label>
            <ComboBox Name="cIsReadOnly" SelectedIndex="0" SelectionChanged="OnReadOnlyChanged" Height="30">
                <ComboBoxItem>True</ComboBoxItem>
                <ComboBoxItem>False</ComboBoxItem>
            </ComboBox>
        </StackPanel>
        <ComboBox Name="cResult" SelectedIndex="0" IsReadOnly="True" IsEditable="True" Grid.Row="1" Grid.Column="2" 
                 Height="30" Width="200" Margin="10">
            <ComboBoxItem>C# programing</ComboBoxItem>
            <ComboBoxItem>a programing</ComboBoxItem>
            <ComboBoxItem>de programing</ComboBoxItem>
            <ComboBoxItem>d programing</ComboBoxItem>
            <ComboBoxItem>Cg programing</ComboBoxItem>
        </ComboBox>
    </Grid>
</Window>

 

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Threading;
using System.Threading;
using System.Windows.Media.Media3D;
using System.Windows.Media.Animation;

namespace MyFirstWpfApplication
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    /// 
    
    public partial class MainWindow : Window
    {
        
        public MainWindow()
        {
            InitializeComponent();
           
        }

        private void OnEditableChanged(object sender, SelectionChangedEventArgs e)
        {
            bool isEditable = this.cIsEditable.SelectedIndex == 0 ? true : false;
            if (cResult != null)
                cResult.IsEditable = isEditable;
        }

        private void OnReadOnlyChanged(object sender, SelectionChangedEventArgs e)
        {
            bool isReadOnly = this.cIsReadOnly.SelectedIndex == 0 ? true : false;
            if (cResult != null)
                cResult.IsReadOnly = isReadOnly;
        }

    }
}

TabControl

TabControl有个属性是TabStripPlacement,这个属性可以取四个值,Top、Left、Bottom和Right,它是用来指定放置Tabcontrol在窗口中的位置的。

View Code
<Window x:Class="MyFirstWpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        x:Name="Window"
        Title="加油" Height="400" Width="500" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="2*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <TabControl TabStripPlacement="Top" Margin="5,2,10,10" Grid.Row="0" Grid.Column="0">
            <TabItem Header="李白" Background="Cyan" FontSize="12">
                <StackPanel>
                    <TextBlock FontWeight="Bold" Margin="0,0,0,20">观庐山瀑布</TextBlock>
                    <TextBlock>
                        日照香炉生紫烟,<LineBreak/>
                        遥看瀑布挂前川。<LineBreak/>
                        飞流直下三千尺,<LineBreak/>
                        疑是银河落九天。<LineBreak/>
                    </TextBlock>
                </StackPanel>
            </TabItem>
        </TabControl>
    </Grid>
</Window>

列表框(ListBox)

 ListBox中至少含有一个ListBoxItem,ListBoxItem从ContentControl中派生出来,因此具有内容控件的特征,ListBox中有一个SelectionMode属性,该属性为枚举类型可取Single、Multiple、Extended三个值。

ListView是ListBox的派生类。ListView和ListBox的不同之处在于,ListView有多列,而且每列都有头。ListView默认的SelectionMode为Extended。

状态条(StatusBar)

状态条用于显示应用程序的当前状态信息,一般放在窗口的底部。

 树形控件TreeView和TreeViewItem

TreeView用来显示具有树形结构的信息。TreeView中的条目为TreeViewItem。TreeViewItem也是一个条目控件,继承自HeaderedItemsControl。树形控件特别容易和XML数据绑定起来,在SOA体系结构中,要大量使用XML。

 文本控件

口令输入框PasswordBox

PasswordBox控件用于输入用户口令,常用属性MaxLength、PasswordChar

 文字输入框(TextBox)

不解释

RichTextBox

TextBox中只能有一种格式,若需要多种方式显示字符,或在字符中插入其他的图像、声音等对象时,就需要使用RichTextBox。

 范围控件(RangeControls

滚动条(ScrollBar

滑动条(Slider)

进展条(ProgressBar)

 

posted @ 2012-07-22 22:14  zscflying  阅读(1675)  评论(2编辑  收藏  举报