【问题标题】:How to use a dependency property defined in custom class?如何使用自定义类中定义的依赖属性?
【发布时间】:2014-02-18 08:06:19
【问题描述】:

我有一个为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 无法解析的异常。

【问题讨论】:

  • 在您的情况下,您需要实现 attached 依赖属性 - MSDN link。并像这样使用:&lt;TextBox local:DependencyProperties.SelectionBegin={...} /&gt;.
  • 我试过了。但现在我得到另一个错误:“不能在'TextBox'类型的'SetSelectionBegin'属性上设置'Binding'。只能在DependencyObject的DependencyProperty上设置'Binding'。”
  • @Sergiu 你在实现custom attached property..时在某个地方犯了一个错误。

标签: c# wpf textbox dependency-properties


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我创建了一个简单的类,看起来应该带有一些注释。通过这个例子,你可以让自己成为剩余的属性。

    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>
    

    对于attached依赖属性设置了WindowInt类型的Width,如果大于零,则TextBox标记为红色背景。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-08
      • 1970-01-01
      • 2013-10-16
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多