更改TextBox的标准行为?

时间:2023-01-25 21:00:24

I have an application that declares textboxes in various places, like in styles and datatemplates, and now I'm in a situation where I need to change every textbox's standard behavior for getting and losing focus.

我有一个应用程序,在各个地方声明文本框,如样式和数据窗口,现在我需要更改每个文本框的标准行为,以获得和失去焦点。

What's a good way to do this?

这样做的好方法是什么?

I was thinking of two solutions: one is to derive a new class from TextBox, which I understand is generally frowned upon. The other is to create some kind of style that uses EventSetters, but since the styles and datatemplates in my application don't have codebehind files I donno how an event will find the appropriate event handler.

我正在考虑两个解决方案:一个是从TextBox派生一个新类,我理解这通常是不满意的。另一种是创建某种使用EventSetters的样式,但由于我的应用程序中的样式和数据窗口没有代码隐藏文件,我不知道事件如何找到适当的事件处理程序。

4 个解决方案

#1


You can create a style that applies to all TextBoxes using the Key property as follows:

您可以使用Key属性创建适用于所有TextBox的样式,如下所示:

<Style x:Key={x:Type TextBox}>
...
</Style>

You can then modify the Template property of the TextBox and use Triggers to add special behavior to the OnGotFocus and OnLostFocus events.

然后,您可以修改TextBox的Template属性,并使用Triggers为OnGotFocus和OnLostFocus事件添加特殊行为。

#2


Based on your feedback, I'd recommend an attached behavior used as follows:

根据您的反馈,我建议使用如下附加行为:

<TextBox b:TextBox.SuppressOnFocus="True"/>

The attached behavior implementation would simply attach to GotFocus and LostFocus and clear/reapply the binding as appropriate.

附加的行为实现将简单地附加到GotFocus和LostFocus,并根据需要清除/重新应用绑定。

#3


Under normal circumstances, I, too, would frown upon subclassing TextBox. In this case, since you are changing the behavior of the TextBox, a subclass may be your best option.

在正常情况下,我也会在继承TextBox时皱眉。在这种情况下,由于您正在更改TextBox的行为,因此子类可能是您的最佳选择。

#4


If you are going to use this functionality in only one project then you can create UserControls which has a TextBox and access the the OnFocus properties. You can also make a Custom WPF Control which derives from a TextBox and then implement the LocusFocus event.

如果要仅在一个项目中使用此功能,则可以创建具有TextBox的UserControl并访问OnFocus属性。您还可以创建一个自定义WPF控件,该控件从TextBox派生,然后实现LocusFocus事件。

I have used the same approach to create a User Control TextBox which performs validation:

我使用相同的方法来创建执行验证的用户控件TextBox:

http://www.highoncoding.com/Articles/578_Creating_WPF_TextBox_UserControl_to_Perform_Custom_Validation.aspx

#1


You can create a style that applies to all TextBoxes using the Key property as follows:

您可以使用Key属性创建适用于所有TextBox的样式,如下所示:

<Style x:Key={x:Type TextBox}>
...
</Style>

You can then modify the Template property of the TextBox and use Triggers to add special behavior to the OnGotFocus and OnLostFocus events.

然后,您可以修改TextBox的Template属性,并使用Triggers为OnGotFocus和OnLostFocus事件添加特殊行为。

#2


Based on your feedback, I'd recommend an attached behavior used as follows:

根据您的反馈,我建议使用如下附加行为:

<TextBox b:TextBox.SuppressOnFocus="True"/>

The attached behavior implementation would simply attach to GotFocus and LostFocus and clear/reapply the binding as appropriate.

附加的行为实现将简单地附加到GotFocus和LostFocus,并根据需要清除/重新应用绑定。

#3


Under normal circumstances, I, too, would frown upon subclassing TextBox. In this case, since you are changing the behavior of the TextBox, a subclass may be your best option.

在正常情况下,我也会在继承TextBox时皱眉。在这种情况下,由于您正在更改TextBox的行为,因此子类可能是您的最佳选择。

#4


If you are going to use this functionality in only one project then you can create UserControls which has a TextBox and access the the OnFocus properties. You can also make a Custom WPF Control which derives from a TextBox and then implement the LocusFocus event.

如果要仅在一个项目中使用此功能,则可以创建具有TextBox的UserControl并访问OnFocus属性。您还可以创建一个自定义WPF控件,该控件从TextBox派生,然后实现LocusFocus事件。

I have used the same approach to create a User Control TextBox which performs validation:

我使用相同的方法来创建执行验证的用户控件TextBox:

http://www.highoncoding.com/Articles/578_Creating_WPF_TextBox_UserControl_to_Perform_Custom_Validation.aspx