我对绑定有些不理解。我有一个用于类型为Object的DataTemplate,它可以正常工作,但是在其中我想创建另一个ListBox并将数据设置为Object的属性之一的数据。我一直在使用Snoop查看数据上下文和对象DataTemplate中ListBox的数据上下文是一个Object,但是ItemsSource存在错误,我不知道为什么。我正在进行ItemsSource={Binding componentList, Mode=TwoWay}操作,而Object具有componentList且componentList是ObservableList。我漏掉了什么?
下面是我的XAML代码:
任何帮助都将不胜感激。谢谢!
下面是我的XAML代码:
<Window.Resources>
<DataTemplate DataType="{x:Type properties:Component}">
<StackPanel>
<TextBlock Text="TEST COMPONENT" />
<ListBox DataContext="{Binding propertyList}" ItemsSource="{Binding propertyList}" />
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type properties:Object}">
<StackPanel>
<TextBlock Text="TEST OBJECT" />
<ListBox ItemsSource="{Binding componentList, Mode=TwoWay}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
以下是我的C#代码:
public class Component
{
public string name;
public ObservableCollection<IProperty> propertyList;
}
public class Object
{
public UnsignedProperty objectID;
public ObservableCollection<Component> componentList;
}
我在代码中创建了一个 ListBox
,并将它的 ItemsSource
设置为对象列表,这会显示我的对象 DataTemplate
,但这就是它的限制所在。
ListBox properties = new ListBox();
ObservableCollection<Properties.Object> t = new ObservableCollection<Properties.Object>();
t.Add(selectedObject); //potentially more objects
properties.ItemsSource = t;
PropertyPane.Content = properties;
任何帮助都将不胜感激。谢谢!