是否有XAML的方式来选择文本框的所有文本点击后双? [英] Is there a way in XAML to select all text in textbox when double clicked?

查看:82
本文介绍了是否有XAML的方式来选择文本框的所有文本点击后双?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法突出纯粹通过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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆