سؤال

I defined a ListView like this:

            <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:

    <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).

  1. How to modify/access the "ItemTemplateSelector" and SelectionMode etc. of the GridView?

  2. Is there a way to access each of the items in the booksGridView?

Thx

هل كانت مفيدة؟

المحلول

There are many ways to do that.

  • The easiest one is to wrap your DataTemplate contents into a UserControl.
  • Another one is to use something like ContainerFromIndex().
  • Then you can also use VisualTreeHelper class to walk the visual tree.
  • Then again you can subclass your ItemsControl and override GetContainerForItemOverride() or PrepareContainerForItemOverride() or
  • use the ItemContainerGenerator property

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.

*Edit As for your additional questions:

  • How to modify/access the "ItemTemplateSelector" and SelectionMode etc. of the GridView?
    You have defined your selector resource and gave it a key of "textbookTemplateSelector", so you can just get it with 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.

  • Is there a way to access each of the items in the booksGridView?
    Yes. Since your DataContext is set as the ItemsSource of your ListView - you can access all the items through that 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.

نصائح أخرى

You can access it by using ItemsSource:

foreach(var book in this.booksGridView.ItemsSource)
{
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top