3

聚焦时如何清除我的文本框?我想以 MVVM 方式执行此操作。如果它有意义 - 我的 TextBox 控件具有与 ViewModel 中的某些属性绑定的 Text 属性。TextBox 显示“50,30 zł”之类的东西。用户选择文本、删除文本和编写新文本很不舒服,所以我想在 Texbox 聚焦时清除旧文本。

4

3 回答 3

9

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

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

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

例如:

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 的父窗口中

<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):

<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>
于 2012-04-21T11:27:26.717 回答
0

文本框1.清除();

它清除文本框中的内容

于 2013-07-25T06:52:20.703 回答
0

@Dmitry Martovoi 给出了很好的回答。

这是相同的解决方案,使用附加属性(而不是混合行为)。附加行为使 XAML 更简单,但 C# 更简单,而 Blend 行为则相反。

XAML:

添加behaviors:MyTextBox.MyClearOnFocusedIfTextEqualsSearch="True"到 TextBox 以使其在收到焦点时自行清除,并且它包含Search.

<Window x:Class="MyApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:behaviors="clr-namespace:MyApp">
    <StackPanel Margin="10">
        <!-- GotFocus="TextBox_GotFocus" -->
        <TextBox x:Name="MyTextBox" 
                 behaviors:MyTextBox.MyClearOnFocusedIfTextEqualsSearch="True" 
                 HorizontalAlignment="Left" 
                 Text="Search" 
                 MinWidth="96" ></TextBox>  
    </StackPanel>
</Window>

和附加的财产:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace MyApp
{
    public class MyTextBox : DependencyObject
    {
        public static readonly DependencyProperty MyClearOnFocusedIfTextEqualsSearchProperty = DependencyProperty.RegisterAttached(
            "MyClearOnFocusedIfTextEqualsSearch",
            typeof (bool),
            typeof(MyTextBox),
            new PropertyMetadata(default(bool), PropertyChangedCallback));

        private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var textBox = dependencyObject as TextBox;
            if (textBox != null)
            {
                if ((bool)dependencyPropertyChangedEventArgs.NewValue == true)
                {
                    textBox.GotFocus += textBox_GotFocus;
                }
            }
        }

        private static void textBox_GotFocus(object sender, RoutedEventArgs e)
        {
            var textBox = sender as TextBox;
            if (textBox != null)
            {
                if (textBox.Text.ToLower() == "search")
                {
                    textBox.Text = "";
                }
            }
        }

        public static void SetMyClearOnFocusedIfTextEqualsSearch(DependencyObject element, bool value)
        {
            element.SetValue(MyClearOnFocusedIfTextEqualsSearchProperty, value);
        }

        public static bool GetMyClearOnFocusedIfTextEqualsSearch(DependencyObject element)
        {
            return (bool)element.GetValue(MyClearOnFocusedIfTextEqualsSearchProperty);
        }
    }
}

我花了几分钟来写这个附加的行为。ReSharper 有一个很棒的宏来执行此操作,如果您键入attachedProperty,它将为您填写大部分代码。

于 2015-01-22T10:10:15.080 回答