1. 程式人生 > 其它 >WPF元件使用之TextBox/TextBlock/Label

WPF元件使用之TextBox/TextBlock/Label

技術標籤:WPFwpftextboxuilabelublock

上篇介紹了簡單的Button元件的使用,帶圖片的按鈕 以及WPF中使用winform元件WPF按鈕

這次來介紹下TextBox/TextBlock/Label 的區別

相同點

三者都可以來用顯示文字內容,都通過設定來調整背景筆刷,字型,樣式,型別,錨點位置,大小縮放透明的等屬性

不同點

最直觀的區別就是TextBox是可編輯元件,可以在執行時進行編輯,常用作修改/輸入時使用

TextBlock和Label是不可編輯的

Label使用的是Content屬性而不是Text屬性,允許託管其他控制元件,可通過ContentTemplate屬性使用模板化的內容。TextBlock只能顯示文字字串

        <Label Margin="-279,-137,494,182">
            <StackPanel Orientation="Vertical" Width="150" Height="171">
                <AccessText Text="Name1:" />
                <AccessText Text="Name2:" />
                <AccessText Text="Name3:" />
                <AccessText Text="Name4:" />
                <AccessText Text="Name5:" />
                <AccessText Text="Name6:" />
                <TextBox Name="texBoxname" Text="TextBoxName"/>
                <TextBlock Name="MyTextBlock"  Background="Blue" Height="25" Text="TextBlockName"/>
            </StackPanel>
        </Label>

訪問鍵

Label 元件可以通過快捷鍵訪問控制項

按住Alt鍵會出現游標,標籤處於控制狀態,執行後按住N鍵可以看到在不同的文字之間移動的焦點。

ElementName 是依據UI元素的Name來進行繫結:

    <StackPanel Margin="33,28,10.333,9.667" >
        <Label Content="_Name:" Target="{Binding ElementName=txtName}" />
        <TextBox Name="txtName" />
        <Label Content="_Name1:" Target="{Binding ElementName=txtName1}" />
        <TextBox Name="txtName1" />
        <Label Content="_Name2:" Target="{Binding ElementName=txtName2}" />
        <TextBox Name="txtName2" />
        <Label Content="_Name3:" Target="{Binding ElementName=txtName3}" />
        <TextBox Name="txtName3" />
        <Label Content="_Name4:" Target="{Binding ElementName=txtName4}" />
        <TextBox Name="txtName4" />
    </StackPanel>