UI⑥Control族控件TextBox

Object->DispatcherObject->DependencyObject->Visual->UIElement->FrameworkElement->Control->TextBoxBase->TextBox


TextBox 

表示一个控件,该控件可用于显示或编辑无格式文本。

TextBox控件在其属性中只能包含无格式文本 Text 。 下图显示了一个示例 TextBox 。

TextBox 屏幕快照
TextBox 示例

TextBox 是由多个封装的组件组成的复合控件。 因此,某些事件不会向上冒泡到包含控件,因为它们由封装的子元素处理。 因此,应用程序开发人员应侦听由前缀 "Preview"  表示 事件的隧道版本。

TextBox 仅支持未格式化文本。 有关需要支持更丰富内容的应用程序,请参阅 RichTextBox 。 对于需要接受密码或其他敏感输入的应用程序,请参阅 PasswordBox 。

TextBox使用 HorizontalContentAlignment 和VerticalContentAlignment 属性在中水平和垂直对齐文本。 TextBox通过HorizontalAlignment 和VerticalAlignment属性在页面布局中对齐  。

TextBox 隐藏周围边框的最佳方式 是将TextBox  的BorderThickness 属性设置 为 0 。

 重要

TextBox 为MouseUp 和MouseDown 冒泡事件提供内置处理 。因此,将不会调用侦听TextBox中MouseUpMouseDown事件的自定义事件处理程序。。如果您需要响应这些事件,请改为监听隧道的PreviewMouseUpPreviewMouseDown事件,或向HandledEventsToo参数注册处理程序(后一种选项仅可通过代码使用)。除非您有意要禁用这些事件的TextBox本机处理,否则不要将事件标记为已处理,并且请注意,这会对控件的UI产生显着影响。

默认情况下,滚动条在TextBox上不可见。若要使滚动条可见,请将VerticalScrollBarVisibilityHorizo​​ntalScrollBarVisibility属性设置为VisibleAuto

通常,应使用TextChanged事件检测TextBoxRichTextBox中的文本何时更改,而不是您期望的KeyDown更改。有关示例,请参见如何:检测文本框中的文本是否已更改

自定义 TextBox 控件

要将相同的属性设置应用于多个TextBox控件,请使用Style属性。您可以修改默认的ControlTemplate以使控件具有唯一的外观。有关创建ControlTemplate的更多信息,请参见通过创建ControlTemplate自定义现有控件的外观。若要查看特定于TextBox的部分和状态,请参见TextBox样式和模板

此控件的依赖项属性可以由控件的默认样式设置。如果使用默认样式设置属性,则当控件出现在应用程序中时,该属性可能会从其默认值更改。默认样式由运行应用程序时使用的桌面主题确定。有关更多信息,请参见默认WPF主题

Note

设置视觉属性仅在该属性同时存在于控件的默认模板中并且通过使用TemplateBinding进行设置时才有效。您可以在通过创建ControlTemplate定制现有控件的外观的更改控件的视觉结构”部分中找到视觉属性列表。

字段
名称备注权限
CharacterCasingProperty标识 CharacterCasing 依赖项属性。public static readonly
MaxLengthProperty标识 MaxLength 依赖项属性。public static readonly
MaxLinesProperty标识 MaxLines 依赖项属性。public static readonly
MinLinesProperty标识 MinLines 依赖项属性。public static readonly
TextAlignmentProperty标识 TextAlignment 依赖项属性。public static readonly
TextDecorationsProperty标识 TextDecorations 依赖项属性。public static readonly
TextProperty标识 Text 依赖项属性。public static readonly
TextWrappingProperty标识 TextWrapping 依赖项属性。public static readonly
属性
名称备注权限
CaretIndex获取或设置插入符号的插入位置索引。get; set;
CharacterCasing获取或设置向文本框中手动输入字符时如何确定字符的大小写格式。get; set;
LineCount获取文本框中的总行数。get;
LogicalChildren获取 TextBox 的逻辑子元素的枚举器。get;
MaxLength获取或设置可在文本框中手动输入的最大字符数。get; set;
MaxLines获取或设置最大可见行数。get; set;
MinLines获取或设置最小可见行数。get; set;
SelectedText获取或设置文本框中当前选择的内容。get; set;
SelectionLength获取或设置一个值,该值指示文本框中当前选择的字符数。get; set;
SelectionStart获取或设置当前选择的起始位置的字符索引。get; set;
Text获取或设置文本框的文本内容。get; set;
TextAlignment获取或设置文本框内容的水平对齐方式。get; set;
TextDecorations获取要应用到文本框的文本修饰。get; set;
TextWrapping获取或设置文本框中文本的换行方式。get; set;
Typography获取文本框的文本内容的当前有效版式变体。get;
方法
名称备注权限
Clear清除文本框中的所有内容。public
GetCharacterIndexFromLineIndex返回指定行中第一个字符的从零开始的字符索引。public
GetCharacterIndexFromPoint返回最接近指定点的字符的索引,该索引从零开始。public
GetFirstVisibleLineIndex返回文本框中当前可见的第一行的行索引。public
GetLastVisibleLineIndex返回文本框中当前可见的最后一行的行索引。public
GetLineIndexFromCharacterIndex返回包含指定字符索引的行的从零开始的行索引。public
GetLineLength返回指定行中的字符数。public
GetLineText返回当前显示在指定的行的文本。public
GetNextSpellingErrorCharacterIndex返回文本框的内容中下一个拼写错误的起始字符索引。public
GetRectFromCharacterIndex返回指定索引处字符的某个边缘的边框。public
GetSpellingError返回与指定字符索引处的任何拼写错误关联的 SpellingError 对象。public
GetSpellingErrorLength返回包括指定字符的任何拼写错误的长度。public
GetSpellingErrorStart返回包括指定字符的任何拼写错误的起始字符索引。public
MeasureOverride按其内容设置文本框的大小。protected
OnCreateAutomationPeer为文本框创建并返回一个 AutomationPeer 对象。protected
OnPropertyChanged当存在于元素上的一个或多个依赖属性的有效值更改时被调用。protected
OnPropertyChanged将指定行索引处的行滚动到视图中。protected
ScrollToLine选择文本框中的文本范围。public
Select选择文本框中的文本范围。public
ShouldSerializeText返回一个值,该值指示在序列化 Text 对象期间,是否应对 TextBox 属性的有效值进行序列化。public
接口
名称备注

IAddChild.AddChild

void IAddChild.AddChild (object value);

IAddChild.AddText

void IAddChild.AddText (string text);

 


XAML范例

<Window
    x:Class="TextBoxDemo.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:TextBoxDemo"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="200"
    Height="350"
    mc:Ignorable="d">
    <StackPanel>
        <TextBox
            MaxLength="2000"
            MaxLines="3"
            MinLines="1"
            Text="This is a TextBox!Represents a control that can be used to display or edit unformatted text.TextBox is a composite control that is composed of several encapsulated components. Consequently, some events do not bubble up to the containing control because they are handled by encapsulated child elements. Because of this, application developers should listen for the tunneling version of an event (denoted by the prefix Preview)."
            TextAlignment="Left"
            TextDecorations="Strikethrough"
            TextWrapping="Wrap"  />
        <Button Content="Add Text" Click="OnAddText"/>
        <Button Content="Select Msg" Click="OnSelectMsg"/>
        <Button Content="手动输入"  Click="OnInput"/>
    </StackPanel>
</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 TextBoxDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void OnAddText(object sender, RoutedEventArgs e)
        {
            foreach (var item in (this.Content as StackPanel).Children)
            {
                if (item is TextBox)
                {
                    (item as TextBox).AppendText( "Text is Added");
                }
            }
        }

        private void OnSelectMsg(object sender, RoutedEventArgs e)
        {
            foreach (var item in (this.Content as StackPanel).Children)
            {
                if (item is TextBox)
                {
                    MessageBox.Show("SelectedText:" + (item as TextBox).SelectedText + "\n"
                        + "SelectionStart:" + (item as TextBox).SelectionStart + "\n"
                        + "SelectionLength:" + (item as TextBox).SelectionLength + "\n");
                }
            }
        }


        private void OnInput(object sender, RoutedEventArgs e)
        {
            foreach (var item in (this.Content as StackPanel).Children)
            {
                if (item is TextBox)
                {
                    (item as TextBox).CaretIndex = 10;
                    (item as TextBox).CharacterCasing = CharacterCasing.Upper;//不会影响以编程方式添加的字符。
                    MessageBox.Show("手动输入!");
                    (item as TextBox).Focus();
                }                   
            }
        }
    }
}

C#范例

<Window
    x:Class="TextBoxDemo.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:TextBoxDemo"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="200"
    Height="350"
    mc:Ignorable="d">
</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 TextBoxDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            StackPanel stackPanel = new StackPanel();
            TextBox textBox = new TextBox
            {
                MaxLength = 2000,
                MaxLines = 3,
                MinLines = 1,
                Text = "This is a TextBox!Represents a control that can be used to display or edit unformatted text.TextBox is a composite control that is composed of several encapsulated components. Consequently, some events do not bubble up to the containing control because they are handled by encapsulated child elements. Because of this, application developers should listen for the tunneling version of an event (denoted by the prefix Preview).",
                TextDecorations = System.Windows.TextDecorations.Strikethrough,
                TextWrapping = System.Windows.TextWrapping.Wrap,
                TextAlignment = TextAlignment.Left,
                
            };
            Button button0 = new Button { Content = "Add Text" };
            button0.Click += OnAddText;
            Button button1 = new Button { Content = "Select Msg" };
            button1.Click += OnSelectMsg;
            Button button2 = new Button { Content = "手动输入" };
            button2.Click += OnInput;

            stackPanel.Children.Add(textBox);
            stackPanel.Children.Add(button0);
            stackPanel.Children.Add(button1);
            stackPanel.Children.Add(button2);


            this.Content = stackPanel;
        }


        private void OnAddText(object sender, RoutedEventArgs e)
        {
            foreach (var item in (this.Content as StackPanel).Children)
            {
                if (item is TextBox)
                {
                    (item as TextBox).AppendText( "Text is Added");
                }
            }
        }

        private void OnSelectMsg(object sender, RoutedEventArgs e)
        {
            foreach (var item in (this.Content as StackPanel).Children)
            {
                if (item is TextBox)
                {
                    MessageBox.Show("SelectedText:" + (item as TextBox).SelectedText + "\n"
                        + "SelectionStart:" + (item as TextBox).SelectionStart + "\n"
                        + "SelectionLength:" + (item as TextBox).SelectionLength + "\n");
                }
            }
        }


        private void OnInput(object sender, RoutedEventArgs e)
        {
            foreach (var item in (this.Content as StackPanel).Children)
            {
                if (item is TextBox)
                {
                    (item as TextBox).CaretIndex = 10;
                    (item as TextBox).CharacterCasing = CharacterCasing.Upper;//不会影响以编程方式添加的字符。
                    MessageBox.Show("手动输入!");
                    (item as TextBox).Focus();
                }                   
            }
        }
    }
}

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值