我不了解有关绑定(bind)的内容。我有一个用于类型对象的 DataTemplate
正在工作,但在那里我想制作另一个 ListBox
并将数据设置为对象的一个属性的数据。我一直在用Snoop查看数据上下文,对象ListBox
中的DataTemplate
的数据上下文是一个Object,但是ItemsSource
有错误,不知道为什么。我在做 ItemsSource={Binding componentList, Mode=TwoWay}
并且一个 Object 有一个 componentList 而 componentList 是一个 ObservableList
。我错过了什么?
这是我的 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;
任何帮助将非常感激。谢谢!
最佳答案
除了我上面的评论:
您不必在代码中创建 ListBox
,在 XAML 中创建它并将集合绑定(bind)到您的 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>
关于c# - ListBox ItemsSource WPF 绑定(bind),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22443918/