简体   繁体   English

如何在Windows应用商店应用中的ListView / GridView中单击项目显示弹出/弹出窗口

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

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. 我正在开发Windows应用商店应用,并希望显示有关在ListView或GridView中单击的项目的一些其他信息。 This information should be shown in a Popup or Flyout (hast do be definded in C#, not in XAML) next to the clicked item. 此信息应显示在单击项旁边的弹出窗口或弹出窗口中(在C#中定义,而不是在XAML中定义)。

The Problem is, that the ItemClick event handler gives no information about the clicked visual item but only about the data item. 问题是,ItemClick事件处理程序不提供有关单击的可视项目的信息,而只提供有关数据项的信息。 Thus I have no information about where to show the Flyout or Popup on screen. 因此,我没有关于在屏幕上显示弹出窗口或弹出窗口的位置的信息。

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 );
}

I've created a solution that works just like the old Windows Phone Toolkit ContextMenuService. 我创建了一个与旧的Windows Phone Toolkit ContextMenuService类似的解决方案。 The MenuFlyoutService. 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: 您的DataTemplate看起来像:

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

All you need to do is get DataContext: 您需要做的就是获取DataContext:

If You have list with items: 如果您有包含项目的列表:

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

And in XAML: 在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: 并在CodeBehind中访问Item,同时单击列表上的按钮:

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 var item是你要找的whar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GridView ItemClick导航到另一页并显示有关单击的项目Windows Store应用程序的详细信息 - GridView ItemClick navigate to another page and show details about the clicked item Windows store app 如何在Windows Store App 8.1 C#中将具有DataTemplate的ListView添加到Button Flyout - How to add ListView with datatemplate to Button Flyout in windows store app 8.1 c# 如何通过单击Windows Store应用程序中的网格视图项来打开弹出窗口? - How to open Popup by clicking on grid view item in windows store app? Gridview自动滚动到Windows应用商店C#XAML中的选定项目 - Gridview Autoscroll to selected item in windows store app C# xaml 如何在gridview中包装文本-Windows Store应用 - how to wrap text in gridview - windows store app 菜单项中的Windows 10 UWP应用程序菜单弹出窗口 - Windows 10 UWP app menu flyout inside menu item 列出windows store 8.1 app中菜单弹出窗口中的所有字体和颜色 - List all fonts and colors in menu flyout in windows store 8.1 app Windows Store App ListView项目数据绑定和可视状态 - Windows Store App ListView item databinding and visual states 关于Windows Store中App C#的ListView中的项目 - About item in listview for app c# in windows store 如何确定GridView中的单击项 - How to determine the clicked item in a gridview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM