如何使用自定义类中定义的依赖项属性?

时间:2014-02-18 08:06:19

标签: c# wpf textbox dependency-properties

我有一个类为TextBox类定义了一些自定义依赖项属性:

public class DependencyProperties:FrameworkElement 
{
    public static readonly DependencyProperty SelectionBeginProperty = DependencyProperty.Register("SelectionBegin", typeof(int), typeof(TextBox),
                                                          new UIPropertyMetadata(0, SelectionStartDependencyPropertyChanged));
    public static readonly DependencyProperty SelectionLengthProperty = DependencyProperty.Register("SelectionLength", typeof(int), typeof(TextBox),
                                                            new UIPropertyMetadata(0, SelectionLengthDependencyPropertyChanged));
    public static readonly DependencyProperty ChildrenProperty = DependencyProperty.Register("Children", typeof (string), typeof (TreeView));

    static DependencyProperties() 
    {

    }
 ...     
}

当我尝试在Xaml中使用这些属性时:

<TextBox Name="TextBox_1735"
         SelectionBegin="{Binding TextBox_1735SelectionBegin, UpdateSourceTrigger=PropertyChanged}" 
         SelectionLength="{Binding TextBox_1735SelectionLength, UpdateSourceTrigger=PropertyChanged}" />

它引发了一个异常,即无法解析属性SelectionBegin

2 个答案:

答案 0 :(得分:0)

您应该寻找的是Attached Properties,因为必须在控件本身中声明标准依赖项属性。您还可以从TextBox继承并在派生类中添加依赖项属性。

答案 1 :(得分:0)

我创建了一个简单的类,看起来应该有一些评论。通过这个例子,你可以自己做剩余的属性。

<强> AttachedProperty

public class DependencyProperties
{
    #region Here put your property declaration

    public static readonly DependencyProperty SelectionBeginProperty;

    public static void SetSelectionBegin(DependencyObject DepObject, int value)
    {
        DepObject.SetValue(SelectionBeginProperty, value);
    }

    public static int GetSelectionBegin(DependencyObject DepObject)
    {
        return (int)DepObject.GetValue(SelectionBeginProperty);
    }

    #endregion

    #region Here in constructor register you property

    static DependencyProperties()
    {
        SelectionBeginProperty = DependencyProperty.RegisterAttached("SelectionBegin", // RegisterAttached
                                                             typeof(int),              // Type of your property
                                                             typeof(DependencyProperties), // Name of your class
                                                             new UIPropertyMetadata(0, SelectionStartDependencyPropertyChanged));
    }

    #endregion

    private static void SelectionStartDependencyPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) 
    {
        // Some logic
        var textBox = sender as TextBox;

        if (textBox == null) 
        {
            return;
        }

        if (e.NewValue is int && ((int)e.NewValue) > 0)
        {
            textBox.Background = Brushes.Red;
        }
    }
}

<强> XAML

<Window x:Class="AttachedPropertyHelp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:AttachedPropertyHelp"
        Name="MyWindow" Title="MainWindow"
        Height="350" Width="525">

    <Grid>
        <TextBox Name="MyTextBox" 
                 local:DependencyProperties.SelectionBegin="{Binding Path=Width, ElementName=MyWindow}" 
                 Width="100"
                 Height="30" />
    </Grid>
</Window>

对于附加依赖项属性设置Window Int类型的宽度,如果它大于零,则TextBox标记为红色背景。