是否有XAML的方式来选择文本框的所有文本点击后双? [英] Is there a way in XAML to select all text in textbox when double clicked?
问题描述
有没有办法突出纯粹通过XAML在文本框中的所有文字,还是有在Xaml.cs做
Is there a way to highlight all text in textbox purely through XAML, or does it have to be done in the Xaml.cs
谢谢!
推荐答案
这是你会做什么:
首先,添加 DoubleClickBehavior.cs
类到你的项目
First, add DoubleClickBehavior.cs
class to your Project.
class DoubleClickBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
AssociatedObject.MouseDoubleClick += AssociatedObjectMouseDoubleClick;
base.OnAttached();
}
protected override void OnDetaching()
{
AssociatedObject.MouseDoubleClick -= AssociatedObjectMouseDoubleClick;
base.OnDetaching();
}
private void AssociatedObjectMouseDoubleClick(object sender, RoutedEventArgs routedEventArgs)
{
AssociatedObject.SelectAll();
}
}
然后在的.xaml
,添加这种行为您的文本框:
Then in .xaml
, add this behavior to your TextBox:
<TextBox>
<i:Interaction.Behaviors>
<local:DoubleClickBehavior/>
</i:Interaction.Behaviors>
</TextBox>
您需要将两个namepsaces添加到您的的.xaml
使用你的行为。 (我的项目的名称是 WpfApplication1
,所以你可能需要改变):
You need to add two namepsaces to your .xaml
to use your behavior. (The name of my project was WpfApplication1
, so you will probably need to change that):
xmlns:local ="clr-namespace:WpfApplication1"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
这就是它。还需要 System.Windows.Interactivity.dll
使用行为
类。
That's it. Also you need System.Windows.Interactivity.dll
to use the Behavior
class.
您可以从的NuGet包下载经理。
这篇关于是否有XAML的方式来选择文本框的所有文本点击后双?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!