将依赖项属性添加到控件 [英] Add dependency property to control

查看:63
本文介绍了将依赖项属性添加到控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Infragistics XamDateTimeEditor控件,我想向其添加一个依赖项属性,以允许开发人员选择在控件获得焦点时选择所有文本。我已经创建了一种用于设置所需行为的样式,但是我希望开发人员根据布尔依赖项属性决定是否应执行该行为。我不确定该怎么做。

I am using the Infragistics XamDateTimeEditor control and I want to add a dependency property to it to allow the developer to choose to have all the text selected when the control gets focus. I have created a style that is used to set the behavior I want but I want the developer to decide if the behavior should be executed based on a boolean dependency property. I am not sure how that is done.

推荐答案

我假设您为此从XamDateTimeEditor继承。

I assume you inherited from XamDateTimeEditor for this.

如果您可以编写引用标准(clr)属性的代码,然后就可以了:

If you can write the code referencing a "standard" (clr) property, then you are good to go:


  1. 声明您的DependencyProperty

  2. 删除后备字段并替换标准属性的实现,以便它访问DependencyProperty而不是后备字段。

  1. declare your DependencyProperty
  2. remove your backing field and replace the implementation of the standard property so that it accesses the DependencyProperty instead of the backing field.

public class MyXamDateTimeEditor : XamDateTimeEditor
{
    public static readonly DependencyProperty IsSelectOnFocusEnabledProperty = 
      DependencyProperty.Register("IsSelectOnFocusEnabled", typeof(bool), 
    typeof(MyXamDateTimeEditor), new UIPropertyMetadata(false));

    public boolIsSelectOnFocusEnabled
    {
        get
        {
            return (bool)GetValue(IsSelectOnFocusEnabledProperty);
        }
        set
        {
            SetValue(IsSelectOnFocusEnabledProperty, value);
        }
    }
}


然后,当您在代码中访问IsSelectOnFocusEnabled时,它将返回Dependency属性的当前值。

Then, when you access IsSelectOnFocusEnabled in your code it will return the current value of the Dependency Property.

您还可以将其设置为接收通知。每当属性更改时,但我不知道您为什么会这样。

You can also set it up to receive notification whenever the property changes, but I don't see why you would in your case.

此技巧还有另一种选择,它不使用继承和附加属性

There is also another option for this trick, which uses no inheritance and an attached property if you'd like.

更新:

好,因为有人提出要求,所以这是一种实现方法对于任何文本框。应该容易转换为用于在另一种控件上执行该操作的任何事件。

OK, since it was requested, here's a way to achieve that for any textbox. It should be easy to translate to whatever event you use to carry that out on another type of control.

    public class TextBoxBehaviors
    {
        public static bool GetIsSelectOnFocusEnabled(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsSelectOnFocusEnabledProperty);
        }

        public static void SetIsSelectOnFocusEnabled(DependencyObject obj, bool value)
        {
            obj.SetValue(IsSelectOnFocusEnabledProperty, value);
        }

        public static readonly DependencyProperty IsSelectOnFocusEnabledProperty =
            DependencyProperty.RegisterAttached("IsSelectOnFocusEnabled", typeof(bool), 
            typeof(TextBoxBehaviors), 
            new UIPropertyMetadata(false, new PropertyChangedCallback(OnSelectOnFocusChange)));

        private static void OnSelectOnFocusChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is TextBox)
            {
                var tb = d as TextBox;
                if ((bool)e.NewValue)
                {
                    tb.GotFocus += new RoutedEventHandler(tb_GotFocus);
                }
                else
                {
                    tb.GotFocus -= new RoutedEventHandler(tb_GotFocus);
                }
            }
        }

        static void tb_GotFocus(object sender, RoutedEventArgs e)
        {
            var tb = sender as TextBox;

            tb.SelectAll();
        }

    }

使用方法如下例如,

<Window x:Class="WpfApplication2.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication2"        
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBox Text="No Select All" x:Name="TextBox1"/>
        <CheckBox Content="Auto Select"
                  Grid.Column="1"
                  IsChecked="{Binding Path=(local:TextBoxBehaviors.IsSelectOnFocusEnabled), ElementName=TextBox1, Mode=TwoWay}" />
        <TextBox Grid.Row="1" Text="djkhfskhfkdssdkj"
                 local:TextBoxBehaviors.IsSelectOnFocusEnabled="true" />
    </Grid>
</Window>

这向您展示了如何设置属性以激活行为以及如何将其绑定到某些东西否则,如果需要。
请注意,此特定示例并不完美(如果您通过它进行制表,如果在控件内部单击,则文本框具有实际上取消选择文本的内部逻辑,但这仅是有关如何将行为附加到控件的示例通过附加属性)。

This shows you how to set up the property to activate the behavior, and how to bind it to something else if need be. Note that this specific example is not perfect (if you tab through it works, if you click inside the control, the textbox has internal logic that actually deselects the text, but that's just an example on how to attach behaviors to controls through attached properties).

这篇关于将依赖项属性添加到控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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