C# ListBox项源WPF绑定
我对绑定有些不了解。我有一个C# ListBox项源WPF绑定,c#,wpf,binding,listbox,itemssource,C#,Wpf,Binding,Listbox,Itemssource,我对绑定有些不了解。我有一个DataTemplate用于正在工作的type对象,但在那里我想制作另一个ListBox,并将数据设置为对象的一个属性的数据。我一直在使用Snoop查看对象DataTemplate中的ListBox的数据上下文和数据上下文,但是ItemsSource有一个错误,我不知道为什么。我正在做ItemsSource={Binding componentList,Mode=TwoWay}并且一个对象有一个componentList,componentList是一个可观察列表。我
DataTemplate
用于正在工作的type对象,但在那里我想制作另一个ListBox
,并将数据设置为对象的一个属性的数据。我一直在使用Snoop查看对象DataTemplate
中的ListBox
的数据上下文和数据上下文,但是ItemsSource
有一个错误,我不知道为什么。我正在做ItemsSource={Binding componentList,Mode=TwoWay}
并且一个对象有一个componentList,componentList是一个可观察列表
。我错过了什么
以下是我的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>
任何帮助都将不胜感激。谢谢 除了我上面的评论之外: 您不必在代码中创建
列表框
,而是在XAML中创建它,并将集合绑定到项资源
(依赖项属性)
像这样:
C#:
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
this.DataContext=this;
添加(新对象());
添加(新对象());
}
私有可观察收集组件;
公共可观测集合组件
{
得到
{
如果(组件==null)
组件=新的ObservableCollection();
返回组件;
}
}
}
XAML:
<Window>
<Grid>
<ListBox ItemsSource="{Binding Components, Mode=OneWay}" />
</Grid>
</Window>
此外,以下是代码的延续:
C#:
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
this.DataContext=this;
ListBox属性=新建ListBox();
ObservableCollection t=新的ObservableCollection();
t、 添加(新对象());//可能还有更多对象
properties.ItemsSource=t;
PropertyPane.Content=属性;
}
}
公共接口IProperty
{
}
公共类组件
{
公共字符串名称;
私人可观测集合财产清单;
公共可观测集合属性列表
{
得到
{
如果(propertyList==null)
propertyList=新的ObservableCollection();
返回属性列表;
}
}
}
公共类对象
{
私有可观察收集组件列表;
公共可观测集合组件列表
{
得到
{
if(componentList==null)
componentList=新的ObservableCollection();
返回组件列表;
}
}
}
XAML:
您必须在绑定中使用CLR属性,您拥有的是字段、带有Setter和Getter的componentList。哇,我知道这很简单。谢谢这使一切都很顺利。你真的需要依赖性吗?或者只是一个得到;并设置;?DependencyProperties是最基本的属性,通常目标内容是DP,而从DataContext绑定到它的任何内容都是CLR属性。只有DP可以是绑定目标,尽管它们也可以是绑定源。常规CLR属性只能由绑定源创建。
ListBox properties = new ListBox();
ObservableCollection<Properties.Object> t = new ObservableCollection<Properties.Object>();
t.Add(selectedObject); //potentially more objects
properties.ItemsSource = t;
PropertyPane.Content = properties;
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;
}
}
}
<Window>
<Grid>
<ListBox ItemsSource="{Binding Components, Mode=OneWay}" />
</Grid>
</Window>
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;
}
}
}
<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>