C# WPF绑定混乱:复合DependencyObject
我有一个DependencyObject类组合,如下所示:C# WPF绑定混乱:复合DependencyObject,c#,user-controls,binding,dependencyobject,C#,User Controls,Binding,Dependencyobject,我有一个DependencyObject类组合,如下所示: public class A : DependencyObject { public AB AB { get { ... } set { ... } } public AB AC { get { ... } set { ... } } } public class AB : DependencyObject { public string Property1 { get { ... } set { ... }
public class A : DependencyObject {
public AB AB { get { ... } set { ... } }
public AB AC { get { ... } set { ... } }
}
public class AB : DependencyObject {
public string Property1 { get { ... } set { ... } }
public string Property2 { get { ... } set { ... } }
public string Property3 { get { ... } set { ... } }
}
public class AC : DependencyObject {
public string Property1 { get { ... } set { ... } }
public string Property2 { get { ... } set { ... } }
}
在A、AB和AC上,所有属性都按照通常的方式执行引用静态属性的典型GetValue和SetValue操作
现在,类A、AB和AC有相应的用户控件AGroupBox、ABGrid、ACGrid。AGroupBox具有根a类属性,ABGrid具有根AB类属性,ACGrid具有根AC类属性
ABGrid和ACGrid都有工作绑定(例如,ABGrid包含一个文本框控件,其文本属性双向绑定到AB的属性1)。我通过创建一个简单窗口并将ABGrid作为窗口的唯一内容子项以及在代码隐藏设置ABGrid.AB=new AB()中验证了这一点;ACGrid.AC=new AC();,的情况相同
问题是当我试图用AGroupBox做类似的事情时。我尝试在XAML中将AGroupBox添加为窗口内容的单个子级,并将AGroupBox.A属性设置为new A(){AB=new AB(),AC=new AC()};控件绑定失败。AB和AC的PropertyN属性具有默认值
对我遗漏的东西有什么见解吗?我该走另一条路吗
编辑:附加注释-如果我将字符串属性添加到a,(String1)并将其绑定到GroupBox的文本部分,则绑定到该属性有效,但不会绑定到a的AC和AB属性
编辑-2:根据David Hay的请求(所有代码都在命名空间wpfStackOverflow中):
A.cs
public class A : DependencyObject {
static public DependencyProperty BProperty { get; private set; }
static public DependencyProperty CProperty { get; private set; }
static public DependencyProperty PropertyProperty { get; private set; }
static A() {
BProperty = DependencyProperty.Register("B", typeof(B), typeof(A));
CProperty = DependencyProperty.Register("C", typeof(C), typeof(A));
PropertyProperty = DependencyProperty.Register("Property", typeof(string), typeof(A));
}
public B B {
get { return (B)GetValue(BProperty); }
set { SetValue(BProperty, value); }
}
public C C {
get { return (C)GetValue(CProperty); }
set { SetValue(CProperty, value); }
}
public string Property {
get { return (string)GetValue(PropertyProperty); }
set { SetValue(PropertyProperty, value); }
}
public A() {
Property = "A's Default Value";
B = new B();
C = new C();
}
}
public class B : DependencyObject {
static public DependencyProperty PropertyProperty { get; private set; }
static B() {
PropertyProperty = DependencyProperty.Register("Property", typeof(string), typeof(B));
}
public string Property {
get { return (string)GetValue(PropertyProperty); }
set { SetValue(PropertyProperty, value); }
}
public B() {
Property = "B's Default Value";
}
}
public class C : DependencyObject {
static public DependencyProperty PropertyProperty { get; private set; }
static C() {
PropertyProperty = DependencyProperty.Register("Property", typeof(string), typeof(C));
}
public string Property {
get { return (string)GetValue(PropertyProperty); }
set { SetValue(PropertyProperty, value); }
}
public C() {
Property = "C's Default Value";
}
}
public partial class AGroupBox : UserControl {
static public DependencyProperty AProperty { get; private set; }
static AGroupBox() {
AProperty = DependencyProperty.Register("A", typeof(A), typeof(AGroupBox));
}
public A A {
get { return (A)GetValue(AProperty); }
set { SetValue(AProperty, value); }
}
public AGroupBox() {
InitializeComponent();
}
}
public partial class BGrid : UserControl {
static public DependencyProperty BProperty { get; private set; }
static BGrid() {
BProperty = DependencyProperty.Register("B", typeof(B), typeof(BGrid));
}
public B B {
get { return (B)GetValue(BProperty); }
set { SetValue(BProperty, value); }
}
public BGrid() {
InitializeComponent();
}
}
public partial class CGrid : UserControl {
static public DependencyProperty CProperty { get; private set; }
static CGrid() {
CProperty = DependencyProperty.Register("C", typeof(C), typeof(CGrid));
}
public C C {
get { return (C)GetValue(CProperty); }
set { SetValue(CProperty, value); }
}
public CGrid() {
InitializeComponent();
}
}
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
aGroupBox.A = new A()
{
Property = "A's Custom Property Value",
B = new B()
{
Property = "B's Custom Property Value"
},
C = new C()
{
Property = "C's Custom Property Value"
}
};
}
}
B.cs
public class A : DependencyObject {
static public DependencyProperty BProperty { get; private set; }
static public DependencyProperty CProperty { get; private set; }
static public DependencyProperty PropertyProperty { get; private set; }
static A() {
BProperty = DependencyProperty.Register("B", typeof(B), typeof(A));
CProperty = DependencyProperty.Register("C", typeof(C), typeof(A));
PropertyProperty = DependencyProperty.Register("Property", typeof(string), typeof(A));
}
public B B {
get { return (B)GetValue(BProperty); }
set { SetValue(BProperty, value); }
}
public C C {
get { return (C)GetValue(CProperty); }
set { SetValue(CProperty, value); }
}
public string Property {
get { return (string)GetValue(PropertyProperty); }
set { SetValue(PropertyProperty, value); }
}
public A() {
Property = "A's Default Value";
B = new B();
C = new C();
}
}
public class B : DependencyObject {
static public DependencyProperty PropertyProperty { get; private set; }
static B() {
PropertyProperty = DependencyProperty.Register("Property", typeof(string), typeof(B));
}
public string Property {
get { return (string)GetValue(PropertyProperty); }
set { SetValue(PropertyProperty, value); }
}
public B() {
Property = "B's Default Value";
}
}
public class C : DependencyObject {
static public DependencyProperty PropertyProperty { get; private set; }
static C() {
PropertyProperty = DependencyProperty.Register("Property", typeof(string), typeof(C));
}
public string Property {
get { return (string)GetValue(PropertyProperty); }
set { SetValue(PropertyProperty, value); }
}
public C() {
Property = "C's Default Value";
}
}
public partial class AGroupBox : UserControl {
static public DependencyProperty AProperty { get; private set; }
static AGroupBox() {
AProperty = DependencyProperty.Register("A", typeof(A), typeof(AGroupBox));
}
public A A {
get { return (A)GetValue(AProperty); }
set { SetValue(AProperty, value); }
}
public AGroupBox() {
InitializeComponent();
}
}
public partial class BGrid : UserControl {
static public DependencyProperty BProperty { get; private set; }
static BGrid() {
BProperty = DependencyProperty.Register("B", typeof(B), typeof(BGrid));
}
public B B {
get { return (B)GetValue(BProperty); }
set { SetValue(BProperty, value); }
}
public BGrid() {
InitializeComponent();
}
}
public partial class CGrid : UserControl {
static public DependencyProperty CProperty { get; private set; }
static CGrid() {
CProperty = DependencyProperty.Register("C", typeof(C), typeof(CGrid));
}
public C C {
get { return (C)GetValue(CProperty); }
set { SetValue(CProperty, value); }
}
public CGrid() {
InitializeComponent();
}
}
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
aGroupBox.A = new A()
{
Property = "A's Custom Property Value",
B = new B()
{
Property = "B's Custom Property Value"
},
C = new C()
{
Property = "C's Custom Property Value"
}
};
}
}
C.cs
public class A : DependencyObject {
static public DependencyProperty BProperty { get; private set; }
static public DependencyProperty CProperty { get; private set; }
static public DependencyProperty PropertyProperty { get; private set; }
static A() {
BProperty = DependencyProperty.Register("B", typeof(B), typeof(A));
CProperty = DependencyProperty.Register("C", typeof(C), typeof(A));
PropertyProperty = DependencyProperty.Register("Property", typeof(string), typeof(A));
}
public B B {
get { return (B)GetValue(BProperty); }
set { SetValue(BProperty, value); }
}
public C C {
get { return (C)GetValue(CProperty); }
set { SetValue(CProperty, value); }
}
public string Property {
get { return (string)GetValue(PropertyProperty); }
set { SetValue(PropertyProperty, value); }
}
public A() {
Property = "A's Default Value";
B = new B();
C = new C();
}
}
public class B : DependencyObject {
static public DependencyProperty PropertyProperty { get; private set; }
static B() {
PropertyProperty = DependencyProperty.Register("Property", typeof(string), typeof(B));
}
public string Property {
get { return (string)GetValue(PropertyProperty); }
set { SetValue(PropertyProperty, value); }
}
public B() {
Property = "B's Default Value";
}
}
public class C : DependencyObject {
static public DependencyProperty PropertyProperty { get; private set; }
static C() {
PropertyProperty = DependencyProperty.Register("Property", typeof(string), typeof(C));
}
public string Property {
get { return (string)GetValue(PropertyProperty); }
set { SetValue(PropertyProperty, value); }
}
public C() {
Property = "C's Default Value";
}
}
public partial class AGroupBox : UserControl {
static public DependencyProperty AProperty { get; private set; }
static AGroupBox() {
AProperty = DependencyProperty.Register("A", typeof(A), typeof(AGroupBox));
}
public A A {
get { return (A)GetValue(AProperty); }
set { SetValue(AProperty, value); }
}
public AGroupBox() {
InitializeComponent();
}
}
public partial class BGrid : UserControl {
static public DependencyProperty BProperty { get; private set; }
static BGrid() {
BProperty = DependencyProperty.Register("B", typeof(B), typeof(BGrid));
}
public B B {
get { return (B)GetValue(BProperty); }
set { SetValue(BProperty, value); }
}
public BGrid() {
InitializeComponent();
}
}
public partial class CGrid : UserControl {
static public DependencyProperty CProperty { get; private set; }
static CGrid() {
CProperty = DependencyProperty.Register("C", typeof(C), typeof(CGrid));
}
public C C {
get { return (C)GetValue(CProperty); }
set { SetValue(CProperty, value); }
}
public CGrid() {
InitializeComponent();
}
}
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
aGroupBox.A = new A()
{
Property = "A's Custom Property Value",
B = new B()
{
Property = "B's Custom Property Value"
},
C = new C()
{
Property = "C's Custom Property Value"
}
};
}
}
AGroupBox.xaml
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpfStackOverflow"
x:Class="wpfStackOverflow.AGroupBox"
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=A}"
Width="300"
Height="72"
>
<GroupBox Header="{Binding Property}">
<StackPanel >
<local:BGrid B="{Binding B}"/>
<local:CGrid C="{Binding C}"/>
</StackPanel>
</GroupBox>
</UserControl>
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="wpfStackOverflow.BGrid"
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=B}"
>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Property"/>
<TextBox Grid.Column="1" Text="{Binding Property}"/>
</Grid>
</UserControl>
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="wpfStackOverflow.CGrid"
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=C}"
>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Property"/>
<TextBox Grid.Column="1" Text="{Binding Property}"/>
</Grid>
</UserControl>
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpfStackOverflow"
x:Class="wpfStackOverflow.Window1"
Width="400"
Height="200"
>
<local:AGroupBox x:Name="aGroupBox" />
</Window>
BGrid.xaml
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpfStackOverflow"
x:Class="wpfStackOverflow.AGroupBox"
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=A}"
Width="300"
Height="72"
>
<GroupBox Header="{Binding Property}">
<StackPanel >
<local:BGrid B="{Binding B}"/>
<local:CGrid C="{Binding C}"/>
</StackPanel>
</GroupBox>
</UserControl>
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="wpfStackOverflow.BGrid"
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=B}"
>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Property"/>
<TextBox Grid.Column="1" Text="{Binding Property}"/>
</Grid>
</UserControl>
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="wpfStackOverflow.CGrid"
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=C}"
>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Property"/>
<TextBox Grid.Column="1" Text="{Binding Property}"/>
</Grid>
</UserControl>
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpfStackOverflow"
x:Class="wpfStackOverflow.Window1"
Width="400"
Height="200"
>
<local:AGroupBox x:Name="aGroupBox" />
</Window>
CGrid.xaml
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpfStackOverflow"
x:Class="wpfStackOverflow.AGroupBox"
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=A}"
Width="300"
Height="72"
>
<GroupBox Header="{Binding Property}">
<StackPanel >
<local:BGrid B="{Binding B}"/>
<local:CGrid C="{Binding C}"/>
</StackPanel>
</GroupBox>
</UserControl>
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="wpfStackOverflow.BGrid"
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=B}"
>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Property"/>
<TextBox Grid.Column="1" Text="{Binding Property}"/>
</Grid>
</UserControl>
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="wpfStackOverflow.CGrid"
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=C}"
>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Property"/>
<TextBox Grid.Column="1" Text="{Binding Property}"/>
</Grid>
</UserControl>
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpfStackOverflow"
x:Class="wpfStackOverflow.Window1"
Width="400"
Height="200"
>
<local:AGroupBox x:Name="aGroupBox" />
</Window>
window1.xaml
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpfStackOverflow"
x:Class="wpfStackOverflow.AGroupBox"
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=A}"
Width="300"
Height="72"
>
<GroupBox Header="{Binding Property}">
<StackPanel >
<local:BGrid B="{Binding B}"/>
<local:CGrid C="{Binding C}"/>
</StackPanel>
</GroupBox>
</UserControl>
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="wpfStackOverflow.BGrid"
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=B}"
>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Property"/>
<TextBox Grid.Column="1" Text="{Binding Property}"/>
</Grid>
</UserControl>
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="wpfStackOverflow.CGrid"
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=C}"
>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Property"/>
<TextBox Grid.Column="1" Text="{Binding Property}"/>
</Grid>
</UserControl>
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpfStackOverflow"
x:Class="wpfStackOverflow.Window1"
Width="400"
Height="200"
>
<local:AGroupBox x:Name="aGroupBox" />
</Window>
尝试将以下内容替换为AGroupBox.xaml
<local:BGrid B="{Binding Path=A.B, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:AGroupBox}}}"/>
<local:CGrid C="{Binding Path=A.C, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:AGroupBox}}}"/>
它没有正确解析这两行的datacontext,因此没有为B和C寻找正确的位置。我们必须看到AGroupBox的XAML,特别是绑定声明,以便在这里提供帮助。我猜想您的绑定语句没有正确设置以访问适当的数据。另外,调试输出中绑定失败消息的读数是多少?谢谢您的帮助!我在VisualStudio的“输出”窗口中查看了调试输出,没有发现任何绑定警告,您是如何确定问题的?关于一个不那么冗长的解决方案有什么想法吗?这里有一个链接,链接到一篇关于绑定调试技术的好文章:我知道有一种更干净的方法可以做到这一点(类似于您最初的尝试),但我现在无法想象它。