如何从在子 class 中创建的依赖项 属性 获取绑定数据

How to get bound data from Dependency property that created in sub class

例如在后台属性我们可以这样做

<cs:CustomControl.Background>
    <SolidColorBrush Color="{Binding BackGround}"/>
</cs:CustomControl.Background>

SolidColorBrush 是一个单独的 class 并包含单独的依赖属性,它工作正常并且后台 属性 正在按预期更新。

我正在尝试在我的 CustomControl 上做同样的概念。

我已经创建了一个像这样属性 具有依赖性的子class

public class SubClass : DependencyObject
{
    public double SubProperty1
    {
        get { return (double)GetValue(SubProperty1Property); }
        set { SetValue(SubProperty1Property, value); }
    }

    // Using a DependencyProperty as the backing store for SubProperty1.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SubProperty1Property =
        DependencyProperty.Register("SubProperty1", typeof(double), typeof(SubClass), new PropertyMetadata(10.0d, OnSubProperty1Changed));

    private static void OnSubProperty1Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        //never called;
    }

    public string SubProperty2
    {
        get { return (string)GetValue(SubProperty2Property); }
        set { SetValue(SubProperty2Property, value); }
    }

    // Using a DependencyProperty as the backing store for SubProperty1.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SubProperty2Property =
        DependencyProperty.Register("SubProperty2", typeof(string), typeof(SubClass), new PropertyMetadata("test", OnSubProperty2Changed));

    private static void OnSubProperty2Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        //never called;
    }
}

在我的自定义工具的主要 class 中,我创建了如下依赖项 属性:

public SubClass MainProperty
{
    get { return (SubClass)GetValue(MainPropertyProperty); }
    set { SetValue(MainPropertyProperty, value); }
}

// Using a DependencyProperty as the backing store for MainProperty.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty MainPropertyProperty =
        DependencyProperty.Register("MainProperty", typeof(SubClass), typeof(CustomControl), new PropertyMetadata((SubClass)null, OnMainPropertyChanged));

private static void OnMainPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    // called when object created only and not called again when the property changed on the UI
}

然后在我的 WPF 中 window 我做了如下操作

<cs:CustomControl.MainProperty>
    <cs:SubClass SubProperty1="{Binding Length}" SubProperty2="{Binding Title}"/>
</cs:CustomControl.MainProperty>

我的代码可以编译,但是依赖属性没有更新,视图模型更新时没有调用回调函数

我怎样才能使这个工作?我想念什么 我希望有详细代码的想法 提前致谢

在 Visual Studio 的输出 Window 中,您应该已经看到类似

的内容

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. ...

调试应用程序时。这是因为您的控件及其 DependencyObject 属性 没有形成逻辑树。

为了构建逻辑树,子类必须派生自Freezable

public class SubClass : Freezable
{
   ...

    protected override Freezable CreateInstanceCore()
    {
        return new SubClass();
    }
}

并且 CustomControl 必须将 SubClass 实例添加到其逻辑子集合中:

private static void OnMainPropertyChanged(
    DependencyObject o, DependencyPropertyChangedEventArgs e)
{
    if (e.OldValue != null)
    {
        ((CustomControl)o).RemoveLogicalChild(e.OldValue);
    }
    if (e.NewValue != null)
    {
        ((CustomControl)o).AddLogicalChild(e.NewValue);
    }
}