聚焦时清除文本框 [英] Clear textbox when focused

查看:50
本文介绍了聚焦时清除文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在文本框获得焦点时清除它?我想以 MVVM 的方式做到这一点.如果它有意义 - 我的 TextBox 控件具有与 ViewModel 中的某些属性绑定的 Text 属性.文本框显示诸如50,30 zł"之类的东西.用户选择文本、删除文本和编写新文本很不舒服,所以我想在 Texbox 聚焦时清除旧文本.

How can I clear my TextBox when it is focused? I want to do this in MVVM way. If it has meaning - my TextBox control has Text property binded with some property in ViewModel. TextBox displays sth like "50,30 zł". It is uncomfortable for user to select text, delete it and write new text, so I want to clear old Text when Texbox is focused.

推荐答案

您可以编写自己的行为甚至控件.我将尝试解释第一个:

You can write your own behavior or even control. I will try to explain the first one:

首先,您应该添加对 System.Windows.Interactivity 程序集的引用.

First, you should a add reference to the System.Windows.Interactivity assembly.

然后创建一个类(这将是行为)并从System.Windows.Interactivity.Behavior派生它.System.Windows.Controls.TextBox>,其中模板化(通用类型)参数是一个控件,其行为应如我所述.

Then create a class (which will be the behavior) and derive it from System.Windows.Interactivity.Behavior< System.Windows.Controls.TextBox>, where templated (generic type) parameter is a control which should behave as I described.

例如:

class ClearOnFocusedBehavior : System.Windows.Interactivity.Behavior<System.Windows.Controls.TextBox>
{
    private readonly RoutedEventHandler _onGotFocusHandler = (o, e) =>
                                                        {
                                                            ((System.Windows.Controls.TextBox) o).Text =
                                                                string.Empty;
                                                        };

    protected override void OnAttached()
    {
        AssociatedObject.GotFocus += _onGotFocusHandler;
    }

    protected override void OnDetaching()
    {
        AssociatedObject.GotFocus -= _onGotFocusHandler;
    }
}

接下来,将以下引用声明放在 xaml 中的父窗口中

Next, put the following reference declaration in your parent window in xaml

<Window x:Class="ManagementSolution.Views.UpdatePersonsWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
    //namespace with ur behaviors
    xmlns:behaviors="clr-namespace:ManagementSolution.Helper.Behaviours"
    //...
</Window>

最后将行为添加到适当的 UI 元素(在我们的例子中为 TextBox):

Finally add the behavior to the appropriate UI element(TextBox in our case):

<TextBox x:Name="PersonFirstNameTextBox"
             Grid.Column="1"
             Margin="5,0"
             HorizontalAlignment="Stretch"
             VerticalAlignment="Center"
             Style="{StaticResource TextBoxValidationStyle}"
             TextWrapping="Wrap"
             d:LayoutOverrides="Width, Height">
        //behavior added as the content
        <i:Interaction.Behaviors>   
            <behaviors:ClearOnFocusedBehavior /> 
        </i:Interaction.Behaviors>
        <TextBox.Text>
            <Binding Path="PersonFirstName"
                     UpdateSourceTrigger="PropertyChanged"
                     ValidatesOnDataErrors="True">
                <!--
                    <Binding.ValidationRules>
                    <rules:SingleWordNameValidationRule />
                    </Binding.ValidationRules>
                -->
            </Binding>
        </TextBox.Text>
    </TextBox>

这篇关于聚焦时清除文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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