How to show Popup/Flyout at clicked item in ListView/GridView in Windows Store App

后端 未结 3 1111
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 11:21

I am working on a Windows Store App and would like to show some additional information about an Item that was clicked in ListView or GridView. This information should be sho

相关标签:
3条回答
  • 2021-01-05 11:30

    All you need to do is get DataContext:

    If You have list with items:

    MyList.ItemSource = new List<Item>();
    

    And in XAML:

    <ListView x:Name="MyList">
      <ListView.ItemTemplate>
      <DataTemplate>
        <Stackpanel>
          <TextBox Text="{Binding Name}" />
          <Button Content="Add sth" Click="AddClick" />
        </Stackpanel>
      </DataTemplate>
    </ListView.ItemTemplate>
    </ListView>
    

    And in CodeBehind to access Item while click on the button on the list:

    private void AddClick(sender, args){
        var senderButton= (Button) sender;
        var item = (Item) sender.DataContext; //Item form the list
    }
    

    var item is whar you are looking for

    0 讨论(0)
  • 2021-01-05 11:31

    I've created a solution that works just like the old Windows Phone Toolkit ContextMenuService. The MenuFlyoutService. You can find the source on my blog. Using the service removes the need to subscribe to event handlers wherever you want to show the menu.

    Your DataTemplate would look something like:

    <StackPanel>
        <local:MenuFlyoutService.MenuFlyout>
            <MenuFlyout>
                <!-- using the Click event -->
                <MenuFlyoutItem Text="reply" Click="OnReplyClicked"/>
    
                <!-- using commanding to DataContext of MenuItem -->
                <MenuFlyoutItem Text="retweet" Command="{Binding RetweetCommand}"/>
    
                <!-- using commanding to DataContext of parent list -->
                <MenuFlyoutItem Text="favorite" 
                            Command="{Binding DataContext.FavoriteCommand, 
                                      ElementName=TweetList}"
                            CommandParameter="{Binding}"/>
            </MenuFlyout>
        </local:MenuFlyoutService.MenuFlyout>
    
        <!-- content for template goes here -->
    </StackPanel>
    
    0 讨论(0)
  • 2021-01-05 11:33

    Use attached Flyout:

    <DataTemplate x:Key="ListItemTemplate">
        <Grid RightTapped="ListRightTapped" Tapped="ListTapped" Background="Transparent">
            <Grid>
                ...
            </Grid>
            <FlyoutBase.AttachedFlyout>
                <Flyout Closed="listFlyout_Closed">
                    <StackPanel>
                        ...
                    </StackPanel>
                </Flyout>
            </FlyoutBase.AttachedFlyout>
        </Grid>
    </DataTemplate>
    

    And the code:

    private void ListRightTapped( object sender, RightTappedRoutedEventArgs e )
    {
        FlyoutBase.ShowAttachedFlyout( sender as FrameworkElement );
    }
    
    0 讨论(0)
提交回复
热议问题