如何在关注时清除我的文本框?我想用MVVM的方式做到这一点。如果它有意义 - 我的TextBox控件将Text属性绑定到ViewModel中的某个属性。 TextBox显示像“50,30zł”。用户选择文本,删除它并编写新文本是不舒服的,所以我想在Texbox集中时清除旧文本。聚焦时清除文本框
Q
聚焦时清除文本框
2
A
回答
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
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
,它会为你填写大部分代码。
相关问题
- 1. 如何在文本框聚焦时变亮文本框?
- 2. 在文本框聚焦事件的局部视图中清除表格数据
- 3. 清除文本框
- 4. 清除TEXTAREA值点击(聚焦状态)
- 5. 清除焦点上的空文本
- 6. 当SelectedIndex == null时清除文本框
- 7. 点击RFID卡时清除文本框?
- 8. 清除焦点上的文本框不工作 - ASP.NET
- 9. 如何清除文本框?
- 10. 防止WPF文本框在聚焦时改变外观
- 11. 当按下按钮时,文本框会聚焦在按钮上?
- 12. 文本框asp.net的清除文本值?
- 13. JavaScript清除文本框DOM只清除顶部框
- 14. HTML清除按钮不清除文本框使用jsp文本框
- 15. 清除文本框和组合框c#
- 16. 当未聚焦时将焦点设置在文本字段
- 17. 如何在清除文本框中的文本时绑定gridview
- 18. 单击文本框内的文本时必须清除
- 19. 删除虚焦边框/聚焦下拉菜单的轮廓
- 20. 当控件聚焦时绘制边框
- 21. UITextField边框在tvOS时聚焦
- 22. 单击/聚焦时删除输入元素的边框
- 23. 如何在文本框未聚焦时丢失wpf中的文本(丢失键盘焦点)?
- 24. 清除并禁用复选框检查时的文本框
- 25. 在清除文本框字段时再次加载选择框
- 26. 使用硒webdriver清除依赖文本框时未启用文本框
- 27. x或在文本框内交叉以清除文本框
- 28. 清除silverlight中的文本框值
- 29. 清除加载的所有文本框
- 30. 清除WebForms中的所有文本框
伟大的解决方案!我以前从未使用过行为。谢谢! – Arvangen 2012-04-21 18:40:01