我不了解有关绑定的内容。我有一个DataTemplate
类型的Object正在工作,但在那里我想创建另一个ListBox
并将数据设置为Object的一个属性的数据。我一直在使用Snoop查看数据上下文,对象ListBox
中的DataTemplate
的数据上下文是一个对象,但ItemsSource
和ItemsSource={Binding componentList, Mode=TwoWay}
出错了我不知道为什么。我正在做ObservableList
而一个Object有一个componentList,而componentList是一个<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>
。我错过了什么?
这是我的XAML代码:
public class Component
{
public string name;
public ObservableCollection<IProperty> propertyList;
}
public class Object
{
public UnsignedProperty objectID;
public ObservableCollection<Component> componentList;
}
我的C#代码:
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;
,但是&#39;停止的地方
{{1}}
任何帮助将不胜感激。谢谢!
答案 0 :(得分:4)
除了我上面的评论:
您不必在代码中创建ListBox
,在XAML中创建它并将集合绑定到您的ItemsSource
(依赖属性)。
像这样:
C#:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
Components.Add(new Object());
Components.Add(new Object());
}
private ObservableCollection<Object> components;
public ObservableCollection<Object> Components
{
get
{
if (components == null)
components = new ObservableCollection<Object>();
return components;
}
}
}
XAML:
<Window>
<Grid>
<ListBox ItemsSource="{Binding Components, Mode=OneWay}" />
</Grid>
</Window>
此外,这是您的代码的延续:
C#:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
ListBox properties = new ListBox();
ObservableCollection<Object> t = new ObservableCollection<Object>();
t.Add(new Object()); //potentially more objects
properties.ItemsSource = t;
PropertyPane.Content = properties;
}
}
public interface IProperty
{
}
public class Component
{
public string name;
private ObservableCollection<IProperty> propertyList;
public ObservableCollection<IProperty> PropertyList
{
get
{
if (propertyList == null)
propertyList = new ObservableCollection<IProperty>();
return propertyList;
}
}
}
public class Object
{
private ObservableCollection<Component> componentList;
public ObservableCollection<Component> ComponentList
{
get
{
if (componentList == null)
componentList = new ObservableCollection<Component>();
return componentList;
}
}
}
XAML:
<DataTemplate DataType="{x:Type local:Component}">
<StackPanel>
<TextBlock Text="TEST COMPONENT" />
<ListBox ItemsSource="{Binding PropertyList, Mode=OneWay}" />
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Object}">
<StackPanel>
<TextBlock Text="TEST OBJECT" />
<ListBox ItemsSource="{Binding ComponentList, Mode=OneWay}" />
</StackPanel>
</DataTemplate>