使用 ItemsSource 和 ItemTemplate 的 WPF ListBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24411699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF ListBox using ItemsSource and ItemTemplate
提问by James Cronen
I'm confused as to how bindings are resolved when I have both an ItemsSourceand an ItemTemplatein a WPF ListBox.
当我在 WPF 中同时拥有 anItemsSource和 an时,我对如何解决绑定感到困惑。ItemTemplateListBox
I have an ObservableCollection<int>called ListOfIndexes. For each index, I want to look up its record in a database table. I hope to do that in the IndexToObjectDescriptionConverter.
我有一个ObservableCollection<int>叫ListOfIndexes. 对于每个索引,我想在数据库表中查找其记录。我希望在IndexToObjectDescriptionConverter.
<ListBox ItemsSource="{Binding ListOfIndexes}"
ItemTemplate="{Binding Converter={StaticResource IndexToObjectDescriptionConverter}}" />
But a breakpoint in the converter is telling me that the value being read in by the ItemTemplatebinding is of the window itself — i.e., the DataContextof the ItemsSourceand ItemsTemplateis the same.
但在转换器中的断点告诉我的价值正在阅读的ItemTemplate结合是窗口本身-即DataContext的ItemsSource和ItemsTemplate是一样的。
Pardon a bit of candidness, but this seems DUMB. The entire point of the ItemTemplateis to render each element within the ItemsSource, so I guess I figured that the DataContextof the ItemTemplatewould be the individual element being rendered.
恕我直言,但这似乎是愚蠢的。的全部意义ItemTemplate在于渲染 中的每个元素ItemsSource,所以我想我认为DataContext的ItemTemplate将是被渲染的单个元素。
So, that said, how do I tell the ItemTemplatethat it should worry about the individual elements represented by the ItemsSourceand not use the entire window's DataContext?
那么,也就是说,我如何告诉ItemTemplate应该担心由 表示的单个元素ItemsSource而不是使用整个窗口的DataContext?
回答by JimBobBennett
You need to use a data template for the ItemTemplate. This is then applied to each item in the list
您需要为 ItemTemplate 使用数据模板。然后将其应用于列表中的每个项目
MSDN docs are here: http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplate(v=vs.110).aspx
MSDN 文档在这里:http: //msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplate(v=vs.110) .aspx
The issue her is about data context scope. When you bind any property on the ListBox, it will use the data context of the ListBox - hence why that data context is being passed to the converter. If you set a data template inside the ItemTemplate, it will apply that template to each item in the list. I guess based on the simple code you've provided you would need to have the converter inside the data template:
她的问题是关于数据上下文范围。当您绑定 ListBox 上的任何属性时,它将使用 ListBox 的数据上下文 - 这就是为什么要将该数据上下文传递给转换器的原因。如果您在 ItemTemplate 中设置数据模板,它将将该模板应用于列表中的每个项目。我想根据您提供的简单代码,您需要在数据模板中安装转换器:
<ListBox ItemsSource="{Binding ListOfIndexes}">
<ListBox.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding Converter={StaticResource IndexToObjectDescriptionConverter}}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
In this case, the ContentControl will be rendered for each item, with that item as it's data context.
在这种情况下,将为每个项目呈现 ContentControl,该项目作为它的数据上下文。
回答by Sheridan
Firstly, I'd advise that you have a good read of the Data Templating Overviewpage in MSDN so that you can get a better understanding of this data binding process.
首先,我建议您仔细阅读MSDN中的数据模板概述页面,以便更好地了解此数据绑定过程。
I want to look up its record in a database table. I hope to do that in the IndexToObjectDescriptionConverter.
我想在数据库表中查找它的记录。我希望在 IndexToObjectDescriptionConverter 中做到这一点。
That is your first mistake. An IValueConverteris responsible for converting data bound values, notaccessing databases. Access your data in your view model and populate public properties with the results. Then data bind those properties to UI controls in the XAML.
那是你的第一个错误。AnIValueConverter负责转换数据绑定值,而不是访问数据库。在您的视图模型中访问您的数据并使用结果填充公共属性。然后数据将这些属性绑定到 XAML 中的 UI 控件。
Pardon a bit of candidness, but this seems DUMB
恕我直言,但这似乎是愚蠢的
Only to those that do not understand the situation.
只对那些不了解情况的人。
how do I tell the ItemTemplate that it should worry about the individual elements represented by the ItemsSource and not use the entire window's DataContext?
我如何告诉 ItemTemplate 它应该担心 ItemsSource 表示的各个元素而不是使用整个窗口的 DataContext?
You don't tell it anything... simply defining the correct XAML is enough:
你什么都不告诉它......只需定义正确的 XAML 就足够了:
In Resources:
在Resources:
<DataTemplate x:Key="SomeDataTemplate">
<!-- The DataContext here is set to an item from the data bound collection -->
</DataTemplate>
In XAML:
在 XAML 中:
<ListBox ItemsSource="{Binding ListOfIndexes}"
ItemTemplate="{StaticResource SomeDataTemplate}" />
That's it.
就是这样。

