[英]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.