locked
How to bind ViewModel command in DataTemplate of GridView RRS feed

  • Question

  • I want to bind ViewModel command in DataTemplate of GridView.

    <UserControl.Resource>
    <CollectionViewSource
                x:Name="groupedItemsViewSource"
                Source="{Binding Groups, Source={StaticResource MainViewModel}}"
                ItemsPath="Items"
                IsSourceGrouped="true"
                />
    </UserControl.Resource>
    
    	    <GridView
                    x:Name="itemGridView"
                    AutomationProperties.AutomationId="ItemGridView"
                    AutomationProperties.Name="Grouped Items"
                    Margin="116,0,40,46"
                    ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}" 
                    ItemTemplate="{StaticResource Standard250x250ItemTemplate}"
                    SelectionMode="Multiple"
                    >
    
                    <GridView.ItemsPanel>
                        <ItemsPanelTemplate>                        
                            <VirtualizingStackPanel Orientation="Horizontal"/>
                        </ItemsPanelTemplate>
                    </GridView.ItemsPanel>
                    <GridView.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.HeaderTemplate>
                                <DataTemplate>
                                    <Grid Margin="1,0,0,6">
                                        <StackPanel Orientation="Horizontal">
                                           
                                            <Button
                                            AutomationProperties.Name="Group Title"
                                            Content="{Binding Name}"
                                            Command ="{Binding Path=ViewModelCommand}"  
                                            CommandParameter="{Binding}"
                                            Style="{StaticResource TextButtonStyle}"/>
                                        </StackPanel>
                                    </Grid>
                                </DataTemplate>
                            </GroupStyle.HeaderTemplate>
                            <GroupStyle.Panel>
                                <ItemsPanelTemplate>
                                    <VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0"/>
                                </ItemsPanelTemplate>
                            </GroupStyle.Panel>
                        </GroupStyle>
                    </GridView.GroupStyle>
                </GridView>
    ViewModel of the page is assigned to MainViewModel. AppBar commands are working but command inside DataTemplate is not working. Any issue in my Xaml?


    CAT

    Wednesday, March 28, 2012 10:21 PM

Answers

  • When using ElementName, it works. It appears that AncestorType is not yet supported.


    CAT

    Thursday, March 29, 2012 12:33 AM

All replies

  • I tried with RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}

    I'm getting Unknown member 'AncestorType' on element 'RelativeSource'?


    CAT

    Thursday, March 29, 2012 12:21 AM
  • When using ElementName, it works. It appears that AncestorType is not yet supported.


    CAT

    Thursday, March 29, 2012 12:33 AM