C# 可以绑定对象列表吗?
我有以下课程:C# 可以绑定对象列表吗?,c#,wpf,xaml,C#,Wpf,Xaml,我有以下课程: public class MyClass { public MyClass() { OtherClass = new List<OtherClass>(); } public List<OtherClass> OtherClass { get; set; } } 以及以下xaml MyView: 我见过其他绑定示例,这些示例表明,通过这种方式绑定路径,即OtherClass.Name可以很好地用于单个对象
public class MyClass
{
public MyClass()
{
OtherClass = new List<OtherClass>();
}
public List<OtherClass> OtherClass { get; set; }
}
以及以下xaml MyView:
我见过其他绑定示例,这些示例表明,通过这种方式绑定路径,即OtherClass.Name可以很好地用于单个对象。但是,我绑定的是一个对象列表,而不是示例中的单个对象,即OtherClass列表
可以绑定对象列表吗?这里您可能要做的是创建一个Items控件,在该控件中,OtherClass列表中的每个项都将由一个项预先呈现。ItemTemplate将指定列表中的每个项目要显示的内容,在您的情况下,ItemTemplate将包含绑定到Name属性的标签。见下文:
<ItemsControl ItemsSource="{Binding OtherClass}" DataType="{x:Type Framework:MyClass}">
<!-- ItemTemplate -->
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Text="{Binding Name}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
请参阅一个更完整的示例这里您可能想做的是创建一个ItemsControl,其中OtherClass列表中的每个项目都将由一个项目预先呈现。ItemTemplate将指定列表中的每个项目要显示的内容,在您的情况下,ItemTemplate将包含绑定到Name属性的标签。见下文:
<ItemsControl ItemsSource="{Binding OtherClass}" DataType="{x:Type Framework:MyClass}">
<!-- ItemTemplate -->
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Text="{Binding Name}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
请参阅更完整的示例如果要为MyClass创建DataTemplate,则需要使用某种形式的ItemsControl来显示OtherClass列表属性
<DataTemplate DataType="{x:Type Framework:MyClass}">
<ItemsControl ItemsSource="{Binding OtherClass}" DisplayMemberPath="Name"/>
</DataTemplate>
编辑
DisplayMemberPath是显示单个属性的最简单方法,但如果要显示OtherClass类中的多个属性,或更改其格式,则需要定义ItemsControl.ItemTemplate,并告诉ItemsControl如何显示每个项目
<ItemsControl ItemsSource="{Binding OtherClass}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<!-- more properties that you want to display -->
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
如果要为MyClass创建DataTemplate,则需要使用某种形式的ItemsControl来显示OtherClass列表属性
<DataTemplate DataType="{x:Type Framework:MyClass}">
<ItemsControl ItemsSource="{Binding OtherClass}" DisplayMemberPath="Name"/>
</DataTemplate>
编辑
DisplayMemberPath是显示单个属性的最简单方法,但如果要显示OtherClass类中的多个属性,或更改其格式,则需要定义ItemsControl.ItemTemplate,并告诉ItemsControl如何显示每个项目
<ItemsControl ItemsSource="{Binding OtherClass}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<!-- more properties that you want to display -->
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
如果要绑定到对象列表,则该列表必须绑定到具有ItemsSource属性的控件。这些类型的控件能够绑定到集合,以便控件的内部DataContext是列表的类型。在您的情况下,如果您绑定到列表中的OtherClass属性,那么DataContext范围将是“OtherClass”类型,您可以在其中显式绑定到其属性 正如其他人提到的,您可以绑定到ItemsControl,但也可以绑定到ListBox、ListView、DataGrid、TreeView等
<!-- Using ItemsControl -->
<ItemsControl ItemsSource="{Binding OtherClass}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<DataTemplate.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</DataTemplate.Resources>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<!-- Using ListBox-->
<ListBox ItemsSource="{Binding OtherClass}">
<ListBox.ItemTemplate>
<DataTemplate>
<DataTemplate.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</DataTemplate.Resources>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
如果要绑定到对象列表,则该列表必须绑定到具有ItemsSource属性的控件。这些类型的控件能够绑定到集合,以便控件的内部DataContext是列表的类型。在您的情况下,如果您绑定到列表中的OtherClass属性,那么DataContext范围将是“OtherClass”类型,您可以在其中显式绑定到其属性 正如其他人提到的,您可以绑定到ItemsControl,但也可以绑定到ListBox、ListView、DataGrid、TreeView等
<!-- Using ItemsControl -->
<ItemsControl ItemsSource="{Binding OtherClass}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<DataTemplate.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</DataTemplate.Resources>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<!-- Using ListBox-->
<ListBox ItemsSource="{Binding OtherClass}">
<ListBox.ItemTemplate>
<DataTemplate>
<DataTemplate.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</DataTemplate.Resources>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Name属性是公共的…只是忘记了它。问题已编辑。@edmastermind29假设了这一点,但只是为了确定而写。你试过这个答案吗?你想要实现什么?是的,结果和我以前的没有什么不同。我以前只看到了显示列表的类型,而不是它的值或它的值的显示列表。我不明白你想显示什么以及MyView是什么样子。假设上下文是MyClass的实例,则此ItemsControl应显示OtherClass列表属性,显示每个项的名称。你有没有试过直接使用ItemsControl而不使用DataTemplate?哇。当我试图水平显示文本时,我将使用标签而不是文本框,使用包装纸而不是堆栈面板,但这种情绪仍然存在。我知道其他答案暗示了你的编辑,但感谢你的超越!Name属性是公共的…只是忘记了它。问题已编辑。@edmastermind29假设了这一点,但只是为了确定而写。你试过这个答案吗?你想要实现什么?是的,结果和我以前的没有什么不同。我以前只看到了显示列表的类型,而不是它的值或它的值的显示列表。我不明白你想显示什么以及MyView是什么样子。假设上下文是MyClass的实例,则此ItemsControl应显示OtherClass列表属性,显示每个项的名称。你有没有试过直接使用ItemsControl而不使用DataTemplate?哇。当我试图水平显示文本时,我将使用标签而不是文本框,使用包装纸而不是堆栈面板,但这种情绪仍然存在。我知道其他答案暗示了你的编辑,但感谢你的超越!我遵循您的示例,但得到一个编译器错误,该错误指出,添加到IDictionary的所有对象都必须有一个Key属性或与之关联的其他类型的Key。对此有什么见解吗?我遵循您的示例,但得到一个编译器错误,其中指出
添加到IDictionary的ll对象必须具有键属性或与之相关联的其他类型的键。对此有什么见解吗?除了另一个答案外,我还得到了以下编译器错误:添加到IDictionary的所有对象都必须有一个Key属性或与之相关联的其他类型的Key。对此有什么见解吗?听起来你在使用字典而不是列表。根据您发布的上述代码,它应该可以工作。还有其他代码你忘了包括吗?我不知道我在哪里使用字典。我使用的是ObservableCollection来显示数据,但我使用的是如上所述的列表。除了另一个答案外,我还得到了以下编译器错误:添加到IDictionary的所有对象都必须具有Key属性或与之相关联的其他类型的Key。对此有什么见解吗?听起来你在使用字典而不是列表。根据您发布的上述代码,它应该可以工作。还有其他代码你忘了包括吗?我不知道我在哪里使用字典。我使用ObservableCollection来显示数据,但我使用的是如上所述的列表。