【问题标题】:How to show Popup/Flyout at clicked item in ListView/GridView in Windows Store App如何在 Windows 应用商店应用程序的 ListView/GridView 中的单击项目上显示弹出/弹出窗口
【发布时间】:2014-04-19 23:12:45
【问题描述】:

我正在开发一个 Windows 应用商店应用程序,并希望显示有关在 ListView 或 GridView 中单击的项目的一些附加信息。此信息应显示在单击项目旁边的弹出窗口或弹出窗口中(必须在 C# 中定义,而不是在 XAML 中)。

问题是,ItemClick 事件处理程序没有提供有关单击的可视项的信息,而仅提供有关数据项的信息。因此,我没有关于在屏幕上的何处显示 Flyout 或 Popup 的信息。

【问题讨论】:

  • 从未使用过商店应用程序,但在 win 表单上很容易,除了事件 args 之外,您还有发送者对象吗?

标签: c# listview windows-store-apps flyout


【解决方案1】:

使用附加的 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>

还有代码:

private void ListRightTapped( object sender, RightTappedRoutedEventArgs e )
{
    FlyoutBase.ShowAttachedFlyout( sender as FrameworkElement );
}

【讨论】:

  • 这个例子是 MS 应该在他们的文档页面上提供的。谢谢。
  • 我也想在我的应用程序中使用这个功能。你能给我完整的例子吗?我试过上面的代码,但它不起作用。
【解决方案2】:

我创建了一个与旧的 Windows Phone Toolkit ContextMenuService 一样工作的解决方案。 MenuFlyoutService。你可以找到the source on my blog。使用该服务无需订阅事件处理程序,无论您想在哪里显示菜单。

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

【讨论】:

    【解决方案3】:

    您需要做的就是获取 DataContext:

    如果您有包含项目的列表:

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

    在 XAML 中:

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

    然后在 CodeBehind 中点击列表上的按钮来访问 Item:

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

    var item 是你要找的东西

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-15
      • 1970-01-01
      • 2014-02-09
      • 2013-02-27
      • 1970-01-01
      • 2011-08-30
      • 1970-01-01
      相关资源
      最近更新 更多