如何在關注時清除我的文本框?我想用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. 聚焦時的UWP文本框背景
- 2. 文本框聚焦時,卻總是暗
- 3. 嘗試在焦點位於文本框時清除錯誤框
- 4. 如何在文本框聚焦時變亮文本框?
- 5. Firefox:當清除文本框的值時丟失焦點
- 6. 在文本框聚焦事件的局部視圖中清除表格數據
- 7. 清除文本框
- 8. 清除文本框
- 9. 清除TEXTAREA值點擊(聚焦狀態)
- 10. 如何在文本框未聚焦時使JavaScript文件關閉?
- 11. 清除焦點上的空文本
- 12. 當SelectedIndex == null時清除文本框
- 13. 着色文本,同時聚焦在
- 14. JS自動填寫表單,文本框清除焦點?
- 15. 清除焦點上的文本框不工作 - ASP.NET
- 16. 當前往下一個文本框時清除文本框
- 17. 清除文本框C#
- 18. 無法清除文本框
- 19. 用JS清除文本框
- 20. 清除只讀文本框
- 21. 文本框不會清除
- 22. 文本框清除問題
- 23. 如何清除文本框?
- 24. 如何清除文本框?
- 25. C#:如何取消以前聚焦的文本框的焦點?
- 26. 當按下按鈕時,文本框會聚焦在按鈕上?
- 27. 防止WPF文本框在聚焦時改變外觀
- 28. 單擊文本框並清除文本?
- 29. 文本框asp.net的清除文本值?
- 30. 當它獲得焦點時清除文本框的函數或子集
偉大的解決方案!我以前從未使用過行爲。謝謝! – Arvangen 2012-04-21 18:40:01