WPF基础五:UI②内容元素Label

 Label

表示控件的文本标签,并提供快捷键(access keys)支持。

此类提供对快捷键的功能和视觉支持 。 它经常用于启用对等控件的快速键盘访问 TextBox 。 若要将分配 Label 到 Control ,请将 Target 属性设置为当用户按下访问键时应获得焦点的控件。 设置目标还会导致 Microsoft UI 自动化使用标签文本作为目标控件的名称。 有关详细信息,请参阅 辅助功能

若要设置访问密钥,请在应为访问键的字符前添加一个下划线。 如果内容有多个下划线字符,则只会将第一个下划线字符转换为访问密钥;其他下划线显示为普通文本。 如果要转换为访问键的下划线不是第一个下划线,请在要转换的下划线前面使用两个连续的下划线。 例如,以下代码包含一个访问键,并显示为 _Hello World:

<Label>__Hello_World</Label>   

由于 H 前面的下划线为2条,因此 W key 注册为访问键。

标签不可设定焦点,并且不是制表位。 有关详细信息,请参阅 焦点概述

Label是 ContentControl ,这意味着它可以包含任何类型的单个对象 (例如字符串、图像或面板) 。 有关更多信息,请参见 ContentControl 类。

自定义标签控件

若要对多个 Label 控件应用相同的属性设置,请使用 Style 属性。 您可以修改 ControlTemplate 默认值,为控件指定独特的外观。 有关创建 ControlTemplate 的详细信息,请参阅 通过创建 System.windows.controls.controltemplate> 自定义现有控件的外观。 若要查看特定于 Label 的部分和状态,请参阅 标签样式和模板

此控件的依赖属性可能由控件的默认样式设置。 如果按默认样式设置属性,则当控件出现在应用程序中时,属性可能会更改为默认值。 默认样式取决于应用程序运行时使用的桌面主题。 有关详细信息,请参阅 默认的 WPF 主题

只有视觉对象属性已存在于控件的默认模板中并且已使用 TemplateBinding 设置时,设置该属性才有效。 在通过创建 ControlTemplate 自定义现有控件的外观一文的更改控件的视觉结构部分可以找到视觉属性列表。


字段

名称备注权限

TargetPropert

标识 Target 依赖项属性public

属性

名称备注权限

Target

获取或设置当用户按下标签的访问键时获得焦点的元素。public
<Label Target=" nameOfExistingElement"/>

方法

名称备注权限

OnCreateAutomationPeer

提供 LabelAutomationPeer 此控件的适当实现,作为 WPF 基础结构的一部分protected

XAML范例

以下范例,Label控件通过助记符(快捷键:Alt+助记符)和Target属性,提供快捷方式获取控件的焦点。

下面例子:Label的First实例的快捷键是Alt+F,Label的Second实例的快捷键是Alt+S。

注册TextBox获取焦点的事件GotFocus。并提供事件的处理方法。

通过Label的快捷键使得Textbox控件获取焦点。

<Window
    x:Class="LabelDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:LabelDemo"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <StackPanel>
            <Label
                Height="40"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                VerticalContentAlignment="Center"
                BorderBrush="Red"
                BorderThickness="2"
                Content="_First" 
                Target="{Binding  ElementName=TextBox1}"/>
            <Label
                Height="40"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                VerticalContentAlignment="Center"
                BorderBrush="Red"
                BorderThickness="2"
                Content="_Second" 
                Target="{Binding  ElementName=TextBox2}"/>
        </StackPanel>
        <StackPanel Grid.Column="1">
            <TextBox
                x:Name="TextBox1"
                Height="40"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                VerticalContentAlignment="Center"
                Text="TextBox1" 
                GotFocus="TextBox1_GotFocus"/>
            <TextBox
                x:Name="TextBox2"
                Height="40"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                VerticalContentAlignment="Center"
                Text="TextBox2"
                GotFocus="TextBox2_GotFocus"/>
        </StackPanel>

    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;

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

        private void TextBox1_GotFocus(object sender, RoutedEventArgs e)
        {
            MessageBox.Show((sender as TextBox).Name+ "获得焦点");
        }

        private void TextBox2_GotFocus(object sender, RoutedEventArgs e)
        {
            MessageBox.Show((sender as TextBox).Name + "获得焦点");
        }
    }
}

运行

当按下Alt+F,或者鼠标左键单击TextBox1等方法,使得TextBox1获取焦点后

当按下Alt+S,或者鼠标左键单击TextBox2等方法,使得TextBox2获取焦点后

UIElement.GotFocus是控件从获取焦点时触发。


C#实例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;

namespace LabelDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Grid grid = (this as Window).Content as Grid;

            ColumnDefinition column1 = new ColumnDefinition();
            ColumnDefinition column2 = new ColumnDefinition();
            grid.ColumnDefinitions.Add(column1);
            grid.ColumnDefinitions.Add(column2);

            //RowDefinition row1 = new RowDefinition();
            //RowDefinition row2 = new RowDefinition();
            //grid.RowDefinitions.Add(row1);
            //grid.RowDefinitions.Add(row2);

            StackPanel stackPanel1 = new StackPanel();
            StackPanel stackPanel2 = new StackPanel();

            Label label1 = new Label();
            Label label2 = new Label();

            TextBox textBox1 = new TextBox();
            TextBox textBox2 = new TextBox();

            AccessText _First = new AccessText();
            AccessText _Second = new AccessText();
            _First.Text = "_First";
            _Second.Text = "_Second";


            label1.Content = _First;
            label1.Target = textBox1;
            label1.Height = 40;
            label1.HorizontalAlignment = HorizontalAlignment.Center;
            label1.VerticalAlignment = VerticalAlignment.Center;
            label1.VerticalContentAlignment = VerticalAlignment.Center;
            label1.BorderBrush = Brushes.Red;
            label1.BorderThickness = new Thickness(2);

            label2.Content = _Second;
            label2.Target = textBox2;
            label2.Height = 40;
            label2.HorizontalAlignment = HorizontalAlignment.Center;
            label2.VerticalAlignment = VerticalAlignment.Center;
            label2.VerticalContentAlignment = VerticalAlignment.Center;
            label2.BorderBrush = Brushes.Red;
            label2.BorderThickness = new Thickness(2);

            textBox1.Name = "TextBox1";
            textBox1.Height = 40;
            textBox1.HorizontalAlignment = HorizontalAlignment.Center;
            textBox1.VerticalAlignment = VerticalAlignment.Center;
            textBox1.VerticalContentAlignment = VerticalAlignment.Center;
            textBox1.Text = "TextBox1";
            textBox1.GotFocus += TextBox1_GotFocus;

            textBox2.Name = "TextBox2";
            textBox2.Height = 40;
            textBox2.HorizontalAlignment = HorizontalAlignment.Center;
            textBox2.VerticalAlignment = VerticalAlignment.Center;
            textBox2.VerticalContentAlignment = VerticalAlignment.Center;
            textBox2.Text = "TextBox2";
            textBox2.GotFocus += TextBox2_GotFocus;

            stackPanel1.Children.Add(label1);
            stackPanel1.Children.Add(label2);

            stackPanel2.Children.Add(textBox1);
            stackPanel2.Children.Add(textBox2);

            grid.Children.Add(stackPanel1);
            grid.Children.Add(stackPanel2);

            Grid.SetColumn(stackPanel2,1);
        }

        private void TextBox1_GotFocus(object sender, RoutedEventArgs e)
        {
            MessageBox.Show((sender as TextBox).Name+ "获得焦点");

        }

        private void TextBox2_GotFocus(object sender, RoutedEventArgs e)
        {
            MessageBox.Show((sender as TextBox).Name + "获得焦点");
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF中,UI元素只能在UI线程上更新,如果您在非UI线程上更新UI元素,就会引发InvalidOperationException异常。因此,如果您需要在后台线程中更新UI元素,您需要使用Dispatcher对象来将更新操作派发到UI线程上。下面是一个使用Dispatcher对象在后台线程中更新UI元素的示例代码: ```csharp using System.Threading.Tasks; using System.Windows; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void button_Click(object sender, RoutedEventArgs e) { Task.Run(() => { // 在后台线程上更新UI元素 Dispatcher.Invoke(() => { label.Content = "正在处理,请稍候..."; }); // 模拟长时间的处理操作 System.Threading.Thread.Sleep(5000); // 在后台线程上更新UI元素 Dispatcher.Invoke(() => { label.Content = "处理完成!"; }); }); } } ``` 在这个示例代码中,我们在button_Click事件处理程序中创建了一个后台任务Task。在任务中,我们使用Dispatcher.Invoke方法将更新操作派发到UI线程上。在派发的委托中,我们更新了label控件的Content属性,以显示正在处理的消息。然后,我们模拟了一个长时间的处理操作,使用Thread.Sleep方法停顿了5秒钟。在处理完成后,我们再次使用Dispatcher.Invoke方法将更新操作派发到UI线程上,更新了label控件的Content属性,以显示处理完成的消息。这样,我们就成功地在后台线程中更新了UI元素

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值