【问题标题】:ListViewItem Context Menu Get Data from ListViewItemListViewItem 上下文菜单 从 ListViewItem 获取数据
【发布时间】:2012-01-09 10:23:16
【问题描述】:

我正在尝试创建一个上下文菜单,以便当您右键单击ListViewItem 时,会向用户显示一个选项列表。问题是;我无法在Click 事件中获取链接到ListViewItem 的引用项。

我认为这可能是因为我将 ContextMenu 放在了 XAML 中的错误位置。我已经搜索和玩了很长时间,但认为这可能与我使用的 DataTemplate 相关,而这些示例不在模板中。

<ListView Margin="0" Name="FileImagesListView"  VerticalAlignment="Top" Grid.Row="0">                             
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <EventSetter Event="Mouse.MouseEnter" Handler="MouseEnterPicFileListItem" />
            <EventSetter Event="Mouse.MouseLeave" Handler="MouseLeavePicFileListItem"/>          
        </Style>      
    </ListView.ItemContainerStyle>

    <ListView.ItemTemplate>
        <DataTemplate>
            <Border BorderBrush="{Binding Path=BorderBrushColourID, Converter={StaticResource BorderColourConverter}}" BorderThickness="3" CornerRadius="2">
                <StackPanel FlowDirection="LeftToRight"  Orientation="Vertical" Margin="3">
                    <Grid>
                        <TextBlock TextAlignment="Center" Text="{Binding Path=TimeAgo}" Margin="0,7" ></TextBlock>
                        <Label Style="{StaticResource CircularLabel}" HorizontalAlignment="Right" Height="35" Margin="0,-8,0,0" Content="{Binding Path=MatchedCount}" Visibility="{Binding Path=MatchedCount, Converter={StaticResource VisibleIfGreaterThanOne}}" ></Label>
                    </Grid>
                    <Image Name="FilePic" Height="Auto" Width="160" Source="{Binding Path=BitmapPicture}"></Image>
                </StackPanel>
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>

    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Margin="3" Orientation="Horizontal"></StackPanel>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>

【问题讨论】:

  • 你如何尝试获得该物品?
  • 你能告诉我们你的上下文菜单 XAML 吗?

标签: wpf listview listviewitem


【解决方案1】:

通常您只需调用myListViewItem.DataContext 并将其转换为应有的内容即可获取数据项。

private void ListViewItem_Click(object sender, EventArgs e)
{
    ListViewItem item = sender as ListViewItem;
    if (item == null) return;

    MyDataItem = item.DataContext as MyDataItem;

    // Do whatever here
}

附带说明,WPF ContextMenus 不与您的应用程序共享相同的 VisualTree,因此尝试将它们绑定到您的主 UI 的工作方式不同。很难判断这是否与您的问题有关,因为我在您的问题中没有看到 ContextMenuClick 事件。

编辑 如果您的 ContextMenu 位于 ListBoxItem 上,那么您需要参考您的 ContextMenu 的 PlacementTarget 以获取 ContextMenu 所附加的 ListBoxItem

【讨论】:

  • 为歧义道歉,我不是 100% 确定 ContextMenu 应该放在 XAML 中的哪个位置。如果我尝试将 ContextMenu 添加为 &lt;ListView.ContextMenu&gt; &lt;ContextMenu&gt; &lt;MenuItem Name="Option1MenuItem" Header="Option1" Click="Option1MenuItem_Click"&gt;&lt;/MenuItem&gt; &lt;MenuItem Header="Option2" &gt;&lt;/MenuItem&gt; &lt;/ContextMenu&gt; &lt;/ListView.ContextMenu&gt;
  • 这里的问题是来自 Option1MenuItem_Click 的发件人是 Controls.MenuItem 类型,它没有在 ListViewItem 中显示引用的项目。我认为你是对的,因为 ContextMenu 的绑定与我习惯的不同。为格式和重复发布道歉,它不适合一条评论。
  • @SpeedBird527 您需要参考您的 ContextMenu 的 PlacementTarget 以获取 ContextMenu 所附加到的 ListBoxItem
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-05
  • 1970-01-01
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多