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.
Answer 1:
你可以写你自己的行为,甚至控制。 我会尽量解释第一种:
首先,你应该一到System.Windows.Interactivity集添加引用。
然后,创建一个类(这将是行为)和从System.Windows.Interactivity.Behavior <System.Windows.Controls.TextBox>,其中模板化(通用型)参数是如I中所述,其应该表现的控制方法推导。
例如:
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>
Answer 2:
textBox1.Clear();
它清除在文本框中的内容
Answer 3:
从@Dmitry Martovoi伟大的答案。
这是相同的解决方案,使用一个附加属性(而不是共混物行为)。 附加的行为作出简单的XAML,但不太简单的C#,而混合行为是相反的。
XAML:
添加behaviors:MyTextBox.MyClearOnFocusedIfTextEqualsSearch="True"
的文本框,如果它接收焦点,它包含要清楚自己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
,它将填补了大多数代码给你。
文章来源: Clear textbox when focused