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

C# 如何使用自定义类中定义的依赖项属性?,c#,wpf,textbox,dependency-properties,C#,Wpf,Textbox,Dependency Properties,我有一个类,它为TextBox类定义了一些自定义依赖项属性: public class DependencyProperties:FrameworkElement { public static readonly DependencyProperty SelectionBeginProperty = DependencyProperty.Register("SelectionBegin", typeof(int), typeof(TextBox),

我有一个类,它为
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

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

我创建了一个简单的类,该类看起来应该类似于带有一些注释的类。通过此示例,您可以使自己成为剩余的属性

附件属性

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;
        }
    }
}
<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>
XAML

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;
        }
    }
}
<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>


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

在您的情况下,您需要实现附加的依赖项属性-。像这样使用:
。我试过了。但现在我又遇到了另一个错误:“不能在“TextBox”类型的“SetSelectionBegin”属性上设置“Binding”。只能在DependencyObject的DependencyProperty上设置“Binding”。@Sergiu您在实现。。