2012-04-21 71 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,它會爲你填寫大部分代碼。