ListBox ItemsSource WPF Binding
本文关键字:Binding WPF ItemsSource ListBox | 更新日期: 2023-09-27 17:50:28
我不太理解绑定。我有一个DataTemplate
类型的对象是工作的,但在那里我想做另一个ListBox
,并设置一个对象的属性之一的数据。我一直在使用Snoop来查看对象DataTemplate
中ListBox
的数据上下文和数据上下文,但是ItemsSource
有一个错误,我不知道为什么。我正在做ItemsSource={Binding componentList, Mode=TwoWay}
和对象有一个componentList和componentList是一个ObservableList
。我错过了什么?
<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;
如有任何帮助,我将不胜感激。谢谢!
除了我上面的评论:
你不必在代码中创建一个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>