use*_*535 4 c# wpf binding listbox itemssource
我对绑定不了解。我有一个DataTemplate正在工作的for类型的对象,但在这里我想做一个ListBox并将类型设置为对象属性之一的数据。我一直在使用Snoop来查看数据上下文,ListBox而Object中的数据上下文DataTemplate是Object,但是出现了错误,ItemsSource我不知道为什么。我在做ItemsSource={Binding componentList, Mode=TwoWay}一个对象有一个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>
Run Code Online (Sandbox Code Playgroud)
和我的C#代码:
public class Component
{
public string name;
public ObservableCollection<IProperty> propertyList;
}
public class Object
{
public UnsignedProperty objectID;
public ObservableCollection<Component> componentList;
}
Run Code Online (Sandbox Code Playgroud)
我ListBox编写了一个in代码并将其设置ItemsSource为Objects列表,并看到了我的Object 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;
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激。谢谢!
除了我上面的评论:
您不必创建ListBoxin代码,无需在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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
XAML:
<Window>
<Grid>
<ListBox ItemsSource="{Binding Components, Mode=OneWay}" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
另外,这是代码的延续:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)