Select 应用程序中所有文本框的 TextBox 中的所有文本
Select all text in TextBox for all textboxes in app
我想让我的 Windows 10 通用应用程序中的所有文本框在获得焦点时自动 select 所有文本。很像这样 here (WPF)。这在 UWP 中可行吗?
有多种方法可以实现这一点,但这里有一个:只需扩展现有的 TextBox 控件并在任何地方使用它。
public class SelectableTextBox : TextBox
{
public SelectableTextBox()
{
this.GotFocus += SelectableTextBox_GotFocus;
}
private void SelectableTextBox_GotFocus(object sender, RoutedEventArgs e)
{
this.SelectAll();
}
}
XAML:
<local:SelectableTextBox Text="This is some text."/>
这是另一种方法。写一个行为!
public class SelectAllTextBoxBehavior : DependencyObject, IBehavior
{
private TextBox textBox;
public DependencyObject AssociatedObject
{
get
{
return this.textBox;
}
}
public void Attach(DependencyObject associatedObject)
{
if (associatedObject is TextBox)
{
this.textBox = associatedObject as TextBox;
this.textBox.GotFocus += TextBox_GotFocus;
}
}
public void Detach()
{
if (this.textBox != null)
{
this.textBox.GotFocus -= TextBox_GotFocus;
}
}
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
this.textBox.SelectAll();
}
}
并将其附加到每个文本框。
<TextBox Text="This is some text.">
<i:Interaction.Behaviors>
<local:SelectAllTextBoxBehavior />
</i:Interaction.Behaviors>
</TextBox>
您需要将 Behaviors SDK 添加到您的项目中才能使用第二个选项。
我会用 AttachedProperty
的附加行为来做到这一点
像这样:一个 class 来保存 bool
类型的 属性 并且在更改时是 attaching/detaching 焦点事件的处理程序
public static class TextBoxAttached
{
public static bool GetAutoSelectable(DependencyObject obj)
{
return (bool)obj.GetValue(AutoSelectableProperty);
}
public static void SetAutoSelectable(DependencyObject obj, bool value)
{
obj.SetValue(AutoSelectableProperty, value);
}
public static readonly DependencyProperty AutoSelectableProperty =
DependencyProperty.RegisterAttached(
"AutoSelectable",
typeof(bool),
typeof(TextBoxAttached),
new PropertyMetadata(false, AutoFocusableChangedHandler));
private static void AutoFocusableChangedHandler(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if(e.NewValue != e.OldValue)
{
if((bool)e.NewValue == true)
{
(d as TextBox).GotFocus += OnGotFocusHandler;
}
else
{
(d as TextBox).GotFocus -= OnGotFocusHandler;
}
}
}
private static void OnGotFocusHandler(object sender, RoutedEventArgs e)
{
(sender as TextBox).SelectAll();
}
}
用法:XAML
<TextBox Text="Test" local:TextBoxAttached.AutoSelectable="True"/>
编辑
您还可以定义默认样式,使所有 TextBox
都可以自动选择
<Page.Resources>
<Style TargetType="TextBox" >
<Setter Property="local:TextBoxAttached.AutoSelectable" Value="True" />
</Style>
</Page.Resources>
我想让我的 Windows 10 通用应用程序中的所有文本框在获得焦点时自动 select 所有文本。很像这样 here (WPF)。这在 UWP 中可行吗?
有多种方法可以实现这一点,但这里有一个:只需扩展现有的 TextBox 控件并在任何地方使用它。
public class SelectableTextBox : TextBox
{
public SelectableTextBox()
{
this.GotFocus += SelectableTextBox_GotFocus;
}
private void SelectableTextBox_GotFocus(object sender, RoutedEventArgs e)
{
this.SelectAll();
}
}
XAML:
<local:SelectableTextBox Text="This is some text."/>
这是另一种方法。写一个行为!
public class SelectAllTextBoxBehavior : DependencyObject, IBehavior
{
private TextBox textBox;
public DependencyObject AssociatedObject
{
get
{
return this.textBox;
}
}
public void Attach(DependencyObject associatedObject)
{
if (associatedObject is TextBox)
{
this.textBox = associatedObject as TextBox;
this.textBox.GotFocus += TextBox_GotFocus;
}
}
public void Detach()
{
if (this.textBox != null)
{
this.textBox.GotFocus -= TextBox_GotFocus;
}
}
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
this.textBox.SelectAll();
}
}
并将其附加到每个文本框。
<TextBox Text="This is some text.">
<i:Interaction.Behaviors>
<local:SelectAllTextBoxBehavior />
</i:Interaction.Behaviors>
</TextBox>
您需要将 Behaviors SDK 添加到您的项目中才能使用第二个选项。
我会用 AttachedProperty
像这样:一个 class 来保存 bool
类型的 属性 并且在更改时是 attaching/detaching 焦点事件的处理程序
public static class TextBoxAttached
{
public static bool GetAutoSelectable(DependencyObject obj)
{
return (bool)obj.GetValue(AutoSelectableProperty);
}
public static void SetAutoSelectable(DependencyObject obj, bool value)
{
obj.SetValue(AutoSelectableProperty, value);
}
public static readonly DependencyProperty AutoSelectableProperty =
DependencyProperty.RegisterAttached(
"AutoSelectable",
typeof(bool),
typeof(TextBoxAttached),
new PropertyMetadata(false, AutoFocusableChangedHandler));
private static void AutoFocusableChangedHandler(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if(e.NewValue != e.OldValue)
{
if((bool)e.NewValue == true)
{
(d as TextBox).GotFocus += OnGotFocusHandler;
}
else
{
(d as TextBox).GotFocus -= OnGotFocusHandler;
}
}
}
private static void OnGotFocusHandler(object sender, RoutedEventArgs e)
{
(sender as TextBox).SelectAll();
}
}
用法:XAML
<TextBox Text="Test" local:TextBoxAttached.AutoSelectable="True"/>
编辑
您还可以定义默认样式,使所有 TextBox
都可以自动选择
<Page.Resources>
<Style TargetType="TextBox" >
<Setter Property="local:TextBoxAttached.AutoSelectable" Value="True" />
</Style>
</Page.Resources>