[英]How to access an item in a DataTemplate?
I defined a ListView like this: 我定义了这样一个ListView:
<ListView x:Name="libraryBooksListView"
AutomationProperties.AutomationId="VideoListView"
AutomationProperties.Name="Videos"
TabIndex="1"
Padding="0,0,4,0"
ItemsSource="{Binding}"
IsSwipeEnabled="False"
ItemTemplate="{StaticResource LibraryBooksItemTemplate}"
ItemContainerStyle="{StaticResource LibraryListViewItemStyle}"
Grid.Column="1"
SelectionMode="None">
</ListView>
and I defined the LibraryBooksItemTemplate like this: 并且我这样定义了LibraryBooksItemTemplate:
<DataTemplate x:Key="LibraryBooksItemTemplate">
<Grid Margin="0">
<GridView x:Name="booksGridView"
AutomationProperties.AutomationId="ItemGridView"
AutomationProperties.Name="Grouped Items"
ItemsSource="{Binding Items}"
ItemTemplateSelector="{StaticResource textbookTemplateSelector}"
SelectionMode="Multiple"
IsItemClickEnabled="True"
ItemClick="booksGridView_ItemClick"
SelectionChanged="booksGridView_SelectionChanged"
IsSwipeEnabled="false"
ScrollViewer.VerticalScrollBarVisibility="Auto">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
</Grid>
</DataTemplate>
The GridView, booksGridView, has multiple items (books). GridView,booksGridView,具有多个项目(书)。
How to modify/access the "ItemTemplateSelector" and SelectionMode etc. of the GridView? 如何修改/访问GridView的“ ItemTemplateSelector”和SelectionMode等?
Is there a way to access each of the items in the booksGridView? 有没有办法访问booksGridView中的每个项目?
Thx 谢谢
There are many ways to do that. 有很多方法可以做到这一点。
DataTemplate
contents into a UserControl
. 最简单的方法是将DataTemplate
内容包装到UserControl
。 ContainerFromIndex()
. 另一个是使用类似ContainerFromIndex()
东西。 VisualTreeHelper
class to walk the visual tree. 然后,您还可以使用VisualTreeHelper
类遍历可视树。 ItemsControl
and override GetContainerForItemOverride()
or PrepareContainerForItemOverride()
or 然后,您可以再次子类化ItemsControl
并重写GetContainerForItemOverride()
或PrepareContainerForItemOverride()
或 ItemContainerGenerator
property 使用ItemContainerGenerator
属性 The imporant thing to note is that your ItemsSource
provides items to the control while the overrides or the generator provide containers to display the items using the ItemTemplates
. 重要的是要注意的是, ItemsSource
向控件提供项目,而覆盖或生成器提供容器以使用ItemTemplates
显示项目。
*Edit As for your additional questions: *编辑您的其他问题:
How to modify/access the "ItemTemplateSelector" and SelectionMode etc. of the GridView? 如何修改/访问GridView的“ ItemTemplateSelector”和SelectionMode等?
You have defined your selector resource and gave it a key of "textbookTemplateSelector", so you can just get it with this.Resources["textbookTemplateSelector"]. 您已经定义了选择器资源,并为其指定了键“ textbookTemplateSelector”,因此可以使用this.Resources [“ textbookTemplateSelector”]来获取它。 SelectionMode
you can bind to the same source DataContext
you bound your ItemsSource
to and change or read it through a binding. 您可以将SelectionMode
绑定到将ItemsSource
绑定到的同一源DataContext
,并通过绑定对其进行更改或读取。
Is there a way to access each of the items in the booksGridView? 有没有办法访问booksGridView中的每个项目?
Yes. 是。 Since your DataContext
is set as the ItemsSource
of your ListView
- you can access all the items through that DataContext
. 由于您的DataContext
被设置为ListView
的ItemsSource
您可以通过该DataContext
访问所有项目。 Each of these items seems to have an Items
property that is bound to your GridView
, so you can access each of these through the Items
property you have defined yourself. 这些项目中的每一个似乎都具有绑定到GridView
的Items
属性,因此您可以通过自己定义的Items
属性来访问每个Items
。
You can access it by using ItemsSource: 您可以使用ItemsSource访问它:
foreach(var book in this.booksGridView.ItemsSource)
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.