我有一个DependencyObject的阶级成分,看起来像如下:WPF绑定的困惑:复合的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 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具有相应的UserControls AGroupBox,ABGrid,ACGrid。 AGroupBox有一个根类属性,ABGrid有一个根AB类属性,ACGrid有一个根AC类属性。
ABGrid和ACGrid都有工作绑定(例如,ABGrid包含Text属性双向绑定到AB的Property1的TextBox控件)。我已通过创建一个简单的窗口并使ABGrid是Window的唯一内容子级来验证此问题,并且后面的代码ABGrid.AB = new AB(); ACGrid.AC = new AC();相同的场景。
问题是当我尝试与AGroupBox类似的时候。我尝试添加AGroupBox作为XAML中Window内容的单个子项,并将AGroupBox.A属性设置为新的A(){AB = new AB(),AC = new AC()};并且控件的绑定失败。 AB和AC具有其PropertyN属性的默认值。
关于我失踪的任何见解?我应该采取不同的路线吗?
编辑:其他评论 - 如果我将字符串属性添加到A,(String1)并将其绑定到GroupBox的文本部分,则对该属性的绑定起作用,但不会对A的AC和AB属性起作用。
EDIT 2:每戴维·海的请求(所有的代码是在命名空间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();
}
}
B.cs
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";
}
}
C.cs
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";
}
}
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>
AGroupBox.xaml.cs
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();
}
}
BGrid.xaml
<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>
BGrid.xaml.cs
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();
}
}
CGrid.xaml
<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>
CGrid.xaml。CS
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();
}
}
window1.xaml
<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.cs
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"
}
};
}
}
我们将要看到的XAML为AGroupBox正确的地方 - 特别是绑定声明 - 为了帮助这里。我的猜测是你的绑定语句没有正确设置来访问适当的数据。另外,在调试输出中读取绑定失败消息是什么? – 2009-12-07 17:34:57