WPF 內建控制項劄記

來源:互聯網
上載者:User

Content controls

判斷Content control是否有content:
使用HasContent屬性
增加此屬性,而不直接使用Content == null 的原因是方便在xaml做出判斷,比如可以根據HasContent增加Trigger

Content Control的呈現:
     調用UIElement的OnRender方法,如果content沒此方法,會調用ToString方法

Button:
      Button.IsDefault; Button.IsCancel

RepeatButton:
    間隔性的觸發click事件。 Delay; Interval ; 預設值SystemParameters.KeyboardDelay and SystemParameters.KeyboardSpeed

ToggleButton & CheckBox:
     IsThreeState標記是否三態。三態時第一次True,第二次null,第三次False

RadioButton:
    同一parent裡的RadioButton被分到同一組,或者指定GroupName屬性(string類型),即使不同parent,在指定GroupName的情況下,也屬於一組。

Label:
    可在按下access key時,把焦點移到Label綁定的控制項上。
    <Label Target="{Binding ElementName=userNameBox}">_User Name:Label>
    <TextBox Name="userNameBox"/>

ToolTip:
    FrameworkElement和FrameworkContentElement定義了ToolTrip屬性。
    ToolTip內容可以自訂,但是不能與之互動。
    ToolTipService提供了一些attached property,方便定義ToolTip的行為,如ToolTipService.ShowDuration等。

Frame:
    與HTML中的Frame一樣。Frame中的元素與Frame外的元素是不相干的,比如屬性繼承,對於Frame裡的元素就不適用了。

Items Control   

ListBox:
    當綁定的是一個較複雜的對象時,可以用DisplayMemberPath來指定綁定的具體是該對象中哪個屬性。
<Windowx:Class="WpfApplication1.Window2"
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"
Title="Window2"Height="300"Width="300">
    <StackPanel>
        <ListBox DisplayMemberPath="DayOfWeek">
            <Button>ButtonButton>
            <ExpanderHeader="Expander"/>
            <sys:DateTime>1/1/2007sys:DateTime>
            <sys:DateTime>1/2/2007sys:DateTime>
            <sys:DateTime>1/3/2007sys:DateTime>
        ListBox>
    StackPanel>
Window>


可以看到DateTimeB的顯示都是正確的,而Button和Expander沒有DayOfWeek屬性,所以顯示String.Empty
Combobox:
    支援敲字母檢索。有兩個屬性可以控制此功能,IsEditable和IsReadonly。
    IsEditable為False時,Combobox樣式和普通的Combobox一樣,設定為True時,Combobox變為類TextBox的樣式。
    IsEditable為True,且IsReadonly為True時,雖然Combobox顯示為TextBox樣式,但是不能敲字元。
    IsEditable為True,且IsReadonly為False時,可以輸入任一字元。
    如果Combobox裡的Item是複雜的組合,要實現檢索,用TextSearch的Attached Property。

…

因為Items結構會變化,可以把TextSearch.TextPath分攤到每個Item裡。這是因為屬性繼承的原則。

橫向排列ListBox:
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
ItemsPanelTemplate>
ListBox.ItemsPanel>
ListBox>

FrameworkElementFactory panelFactory =
new FrameworkElementFactory(typeof(VirtualizingStackPanel));
panelFactory.SetValue(VirtualizingStackPanel.OrientationProperty,
Orientation.Horizontal);
myListBox.ItemsPanel = new ItemsPanelTemplate(panelFactory);

平滑滾動Listbox:
    ScrollViewer.CanContentScroll

ListBox排序:
// Clear any existing sorting first
myItemsControl.Items.SortDescriptions.Clear();
// Sort by the Content property
myItemsControl.Items.SortDescriptions.Add(
new SortDescription(“Content”, ListSortDirection.Ascending));

ListView:
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Header="Date"/>
<GridViewColumn Header="Day of Week"
DisplayMemberBinding"{Binding DayOfWeek}"/>
<GridViewColumn Header="Year" DisplayMemberBinding="{Binding Year}"/>
GridView>
ListView.View>
<sys:DateTime>1/1/2007sys:DateTime>
<sys:DateTime>1/2/2007sys:DateTime>
<sys:DateTime>1/3/2007sys:DateTime>
ListView>
 
GridView不支援排序,需要自己用SortDescriptions實現

ToolBar:
    多個ToolBar可以放在ToolBarTray中。

Range Controls
ProgressBar:
    IsIndeterminate:設為True時,顯示為連續滾動
    Orientation:方向,橫豎

Slider:

Text and Ink Controls

TextBox:
     允許多行:AcceptsReturn = True
     自動換行:TextWrapping –>Wrap or WrapWithOverflow。 Wrap不會截斷字,WrapWithOverflow而截斷。
     拼字檢查:SpellCheck.IsEnabled = True, 這是個attached property,可是不生效,不知道是不是和地區設定有關。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.