附加行为在Silverlight中的所有文本框 [英] Attach behaviour to all TextBoxes in Silverlight

查看:207
本文介绍了附加行为在Silverlight中的所有文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能行为附加到文本框都在Silverlight应用程序?



我需要简单的功能添加到所有的文本框。
(选择焦点事件的所有文字)

 无效Target_GotFocus(对象发件人,System.Windows.RoutedEventArgs E)
{
Target.SelectAll();
}


解决方案

您可以覆盖默认风格在您的应用程序文本框。然后在这个风格,你可以用一些方法来申请用(一般采用附加属性)二传手行为



这会是这样的:

 < Application.Resources> 
<风格的TargetType =文本框>
< setter属性=本地:TextBoxEx.SelectAllOnFocusVALUE =真/>
< /样式和GT;
< /Application.Resources>



行为实施:

 公共类TextBoxSelectAllOnFocusBehavior:行为<&文本框GT; 
{
保护覆盖无效OnAttached()
{
base.OnAttached();

this.AssociatedObject.GotMouseCapture + = this.OnGotFocus;
this.AssociatedObject.GotKeyboardFocus + = this.OnGotFocus;
}

保护覆盖无效OnDetaching()
{
base.OnDetaching();

this.AssociatedObject.GotMouseCapture - = this.OnGotFocus;
this.AssociatedObject.GotKeyboardFocus - = this.OnGotFocus;
}

公共无效OnGotFocus(对象发件人,EventArgs参数)
{
this.AssociatedObject.SelectAll();
}
}

和附加属性,以帮助我们的行为:

 公共静态类TextBoxEx 
{
公共静态布尔GetSelectAllOnFocus(DependencyObject的OBJ)
{
回报(布尔)obj.GetValue(SelectAllOnFocusProperty);
}
公共静态无效SetSelectAllOnFocus(OBJ的DependencyObject,布尔值)
{
obj.SetValue(SelectAllOnFocusProperty,值);
}
公共静态只读的DependencyProperty SelectAllOnFocusProperty =
DependencyProperty.RegisterAttached(SelectAllOnFocus的typeof(布尔)的typeof(TextBoxSelectAllOnFocusBehaviorExtension),新PropertyMetadata(假,OnSelectAllOnFocusChanged));


私有静态无效OnSelectAllOnFocusChanged(DependencyObject的发件人,DependencyPropertyChangedEventArgs参数)
{
VAR行为= Interaction.GetBehaviors(寄件人);

//删除现有的行为实例
的foreach(VAR老在behaviors.OfType< TextBoxSelectAllOnFocusBehavior方式>()ToArray的())
behaviors.Remove(旧);

如果((布尔)args.NewValue)
{
//创建一个新的行为,重视目标
VAR行为=新TextBoxSelectAllOnFocusBehavior();

//应用行为
behaviors.Add(行为);
}
}
}


Is it possible to attach behavior to all TextBoxes in Silverlight application?

I need to add simple functionality to all text boxes. (select all text on focus event)

 void Target_GotFocus(object sender, System.Windows.RoutedEventArgs e)
    {
        Target.SelectAll();
    }

解决方案

You can override the default style for TextBoxes in your app. Then in this style, you can use some approach to apply a behavior with a setter (generally using attached properties).

It would something like this:

<Application.Resources>
    <Style TargetType="TextBox">
        <Setter Property="local:TextBoxEx.SelectAllOnFocus" Value="True"/>
    </Style>
</Application.Resources>

The behavior implementation:

public class TextBoxSelectAllOnFocusBehavior : Behavior<TextBox>
{
    protected override void OnAttached()
    {
        base.OnAttached();

        this.AssociatedObject.GotMouseCapture += this.OnGotFocus;
        this.AssociatedObject.GotKeyboardFocus += this.OnGotFocus;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();

        this.AssociatedObject.GotMouseCapture -= this.OnGotFocus;
        this.AssociatedObject.GotKeyboardFocus -= this.OnGotFocus;
    }

    public void OnGotFocus(object sender, EventArgs args)
    {
        this.AssociatedObject.SelectAll();
    }
}

And the attached property to help us apply the behavior:

public static class TextBoxEx
{
    public static bool GetSelectAllOnFocus(DependencyObject obj)
    {
        return (bool)obj.GetValue(SelectAllOnFocusProperty);
    }
    public static void SetSelectAllOnFocus(DependencyObject obj, bool value)
    {
        obj.SetValue(SelectAllOnFocusProperty, value);
    }
    public static readonly DependencyProperty SelectAllOnFocusProperty =
        DependencyProperty.RegisterAttached("SelectAllOnFocus", typeof(bool), typeof(TextBoxSelectAllOnFocusBehaviorExtension), new PropertyMetadata(false, OnSelectAllOnFocusChanged));


    private static void OnSelectAllOnFocusChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
    {
        var behaviors = Interaction.GetBehaviors(sender);

        // Remove the existing behavior instances
        foreach (var old in behaviors.OfType<TextBoxSelectAllOnFocusBehavior>().ToArray())
            behaviors.Remove(old);

        if ((bool)args.NewValue)
        {
            // Creates a new behavior and attaches to the target
            var behavior = new TextBoxSelectAllOnFocusBehavior();

            // Apply the behavior
            behaviors.Add(behavior);
        }
    }
}

这篇关于附加行为在Silverlight中的所有文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆