简体   繁体   English

如何访问DataTemplate中的项目?

[英]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,具有多个项目(书)。

  1. How to modify/access the "ItemTemplateSelector" and SelectionMode etc. of the GridView? 如何修改/访问GridView的“ ItemTemplateSelector”和SelectionMode等?

  2. Is there a way to access each of the items in the booksGridView? 有没有办法访问booksGridView中的每个项目?

Thx 谢谢

There are many ways to do that. 有很多方法可以做到这一点。

  • The easiest one is to wrap your DataTemplate contents into a UserControl . 最简单的方法是将DataTemplate内容包装到UserControl
  • Another one is to use something like ContainerFromIndex() . 另一个是使用类似ContainerFromIndex()东西。
  • Then you can also use VisualTreeHelper class to walk the visual tree. 然后,您还可以使用VisualTreeHelper类遍历可视树。
  • Then again you can subclass your ItemsControl and override GetContainerForItemOverride() or PrepareContainerForItemOverride() or 然后,您可以再次子类化ItemsControl并重写GetContainerForItemOverride()PrepareContainerForItemOverride()
  • use the 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被设置为ListViewItemsSource您可以通过该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. 这些项目中的每一个似乎都具有绑定到GridViewItems属性,因此您可以通过自己定义的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM