XAML 自定义依赖项属性找不到控制FrameworkElement或FrameworkContentElement异常

mlmc2os5  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(102)

我正在处理一个从TextBox派生的UserControl
我有自定义DependencyProperty HelperText这样:

public static readonly DependencyProperty HelperTextProperty = DependencyProperty.Register("HelperText", typeof(ICTextBoxHelperText), typeof(ICTextBox));
public ICTextBoxHelperText HelperText
{
    get { return (ICTextBoxHelperText)GetValue(HelperTextProperty); }
    set { SetValue(HelperTextProperty, value); }
}

字符串
这是类类型ICTextBoxHelperText,并有几个属性,我需要在用户界面。

public class ICTextBoxHelperText : DependencyObject
{
    public static readonly DependencyProperty DefaultProperty = DependencyProperty.Register("Default", typeof(string), typeof(ICTextBoxHelperText));
    public static string GetDefault(DependencyObject d) => (string)d.GetValue(DefaultProperty);
    public static void SetDefault(DependencyObject d, string value) { d.SetValue(DefaultProperty, value); }

    public static readonly DependencyProperty InvalidProperty = DependencyProperty.Register("Invalid", typeof(string), typeof(ICTextBoxHelperText));
    public static string GetInvalid(DependencyObject d) => (string)d.GetValue(InvalidProperty);
    public static void SetInvalid(DependencyObject d, string value) { d.SetValue(InvalidProperty, value); }

    public static readonly DependencyProperty ValidatedProperty = DependencyProperty.Register("Validated", typeof(string), typeof(ICTextBoxHelperText));
    public static string GetValidated(DependencyObject d) => (string)d.GetValue(ValidatedProperty);
    public static void SetValidated(DependencyObject d, string value) { d.SetValue(ValidatedProperty, value); }

    public static readonly DependencyProperty AutoValidatedProperty = DependencyProperty.Register("AutoValidated", typeof(string), typeof(ICTextBoxHelperText));
    public static string GetAutoValidated(DependencyObject d) => (string)d.GetValue(AutoValidatedProperty);
    public static void SetAutoValidated(DependencyObject d, string value) { d.SetValue(AutoValidatedProperty, value); }
}


下面是UI实现:

<!-- TextBox -->
<tx:ICTextBox x:Name="icTextBox"
              Width="240"
              Margin="0,20,0,0"
              Padding="0"
              VerticalAlignment="Center">

    <!-- Helper Text -->
    <tx:ICTextBox.HelperText>
        <tx:ICTextBoxHelperText Default="{Binding ElementName=icTextBox, Path=Text, UpdateSourceTrigger=PropertyChanged}"
                                Invalid="Invalid"
                                Validated="Validated"
                                AutoValidated="AutoValidated"/>
    </tx:ICTextBox.HelperText>
</tx:ICTextBox>


ICTextBoxHelperText的静态值按预期工作并更新UI。
问题出在绑定上。它根本不工作,我有这个XAML绑定异常Cannot find governing FrameworkElement or FrameworkContentElement for target element
据我所知,这一定是这个ICTextBoxHelperText没有DataContext的问题,但HelperText是在FrameworkElement内部声明的,这是我从TextBox派生的控件。
这甚至有可能有一个DependencyPropertyDependencyObject类型吗?我这样做了,所以我会有一些类似类型的DependencyProperties很好的分组,而不是一个houndred松散的DependencyPropertiesUserControl

k7fdbhmy

k7fdbhmy1#

在我看来不寻常的是你如何实现你的自定义依赖对象类ICTextBoxHelperText。你使用Register将它们注册为普通的依赖属性,但是Getters和Setters的语法是附加语法(使用GetNNN/SetNNN方法对而不是普通属性)。
然而在XAML中,你似乎可以像访问“普通”非附加属性一样访问它们。此外,没有理由让它们附加属性。我建议重写ICTextBoxHelperText类并从Freezable派生,这是一个轻量级实现,保证链接到可视化树的DataContext中-而DependencyObject继承则没有。由于您的类只包含额外的数据,但没有控件模板/自己的可视化表示,因此从Control或UserControl派生它没有意义。
所以我建议重写ICTextBoxHelperText,看看它是否解决了缺少DataContext的问题。

public class ICTextBoxHelperText : Freezable
{
    public static readonly DependencyProperty DefaultProperty =
          DependencyProperty.Register(nameof(Default), typeof(string), typeof(ICTextBoxHelperText));
    
    public static string Default
    {
        get => (string)GetValue(DefaultProperty);
        set => SetValue(DefaultProperty, value); 
    }

    public static readonly DependencyProperty InvalidProperty =
          DependencyProperty.Register(nameof(Invalid), typeof(string), typeof(ICTextBoxHelperText));

    public static string Invalid
    {
        get => (string)GetValue(InvalidProperty);
        set => SetValue(InvalidProperty, value); 
    }

    public static readonly DependencyProperty ValidatedProperty =
          DependencyProperty.Register(nameof(Validated), typeof(string), typeof(ICTextBoxHelperText));

    public static string Validated
    {
        get => (string)GetValue(ValidatedProperty);
        set => SetValue(ValidatedProperty, value); 
    }

    public static readonly DependencyProperty AutoValidatedProperty =
          DependencyProperty.Register(nameof(AutoValidated), typeof(string), typeof(ICTextBoxHelperText));

    public static string AutoValidated
    {
        get => (string)GetValue(AutoValidatedProperty);
        set => SetValue(AutoValidatedProperty, value); 
    }

    // required override for Freezables
    protected override Freezable CreateInstanceCore() => new ICTextBoxHelperText();
}

字符串

相关问题