2012-04-21 72 views
2

如何在关注时清除我的文本框?我想用MVVM的方式做到这一点。如果它有意义 - 我的TextBox控件将Text属性绑定到ViewModel中的某个属性。 TextBox显示像“50,30zł”。用户选择文本,删除它并编写新文本是不舒服的,所以我想在Texbox集中时清除旧文本。聚焦时清除文本框

回答

7

你可以编写自己的行为甚至控制。 我会尽量解释第一个:

首先,你应该在添加参考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> 
+0

伟大的解决方案!我以前从未使用过行为。谢谢! – Arvangen 2012-04-21 18:40:01

0

textBox1.Clear();

它清除在文本框中的内容从@Dmitry Martovoi

+0

另一种方式是textBox1 =“”; – 2013-07-25 06:54:34

0

伟大的答案。

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

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,它会为你填写大部分代码。