ListViewItem上下文菜单从ListViewItem获取数据 [英] ListViewItem Context Menu Get Data from ListViewItem

查看:76
本文介绍了ListViewItem上下文菜单从ListViewItem获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个上下文菜单,以便在右键单击 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"/></样式></ListView.ItemContainerStyle>< ListView.ItemTemplate>< DataTemplate>< Border BorderBrush ="{Binding Path = BorderBrushColourID,Converter = {StaticResource BorderColourConverter}}""BorderThickness =" 3"CornerRadius =" 2>< StackPanel FlowDirection ="LeftToRight" Orientation ="Vertical" Margin ="3"><网格>< TextBlock TextAlignment ="Center" Text ="{Binding Path = TimeAgo}" Margin ="0,7"></TextBlock><标签样式="{StaticResource CircularLabel}" Horizo​​ntalAlignment ="Right" Height ="35" Margin ="0,-8,0,0" Content ="{Binding Path = MatchedCount}" Visibility ="{Binding Path =MatchedCount,Converter = {StaticResource VisibleIfGreaterThanOne}}></Label></Grid><图片名称="FilePic" Height =自动"宽度="160"源="{Binding Path = BitmapPicture}"></Image></StackPanel></Border></DataTemplate></ListView.ItemTemplate>< ListView.ItemsPanel>< ItemsPanelTemplate>< StackPanel Margin ="3" Orientation ="Horizo​​ntal"></StackPanel></ItemsPanelTemplate></ListView.ItemsPanel></ListView> 

解决方案

通常,您可以通过调用 myListViewItem.DataContext 并将其转换为应有的内容来获取数据项.

  private void ListViewItem_Click(对象发送者,EventArgs e){ListViewItem item =发送者为ListViewItem;如果(item == null)返回;MyDataItem = item.DataContext作为MyDataItem;//在这里做什么} 

请注意,WPF ContextMenus与您的应用程序共享的VisualTree不同,因此尝试将它们绑定到主UI的工作方式有所不同.很难确定这是否与您的问题有关,因为在您的问题中没有看到 ContextMenu Click 事件.

修改如果您的ContextMenu位于ListBoxItem上,则需要引用ContextMenu的 PlacementTarget 来获取 ContextMenu 附加到的 ListBoxItem

I'm trying to create a context menu, so that when you right click the ListViewItem, the user is presented with a list of options. The problem is; I can't get the refrenced item linked to the ListViewItem in the Click event.

I think it may be because I'm putting the ContextMenu in the wrong place in my XAML. I have been searching and playing around for ages, but think it could have something to do with the DataTemplate I'm using where the examples weren't in templates.

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

解决方案

Usually you can get the data item by just calling myListViewItem.DataContext and casting it into whatever it should be.

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
}

As a side note, WPF ContextMenus do not share the same VisualTree as your application, so trying to bind them to your main UI works differently. Its hard to tell if that is related to your problem because I see no ContextMenu or Click event in your question.

Edit If your ContextMenu is on a ListBoxItem, then you need to refer to your ContextMenu's PlacementTarget to get the ListBoxItem that the ContextMenu is attached to

这篇关于ListViewItem上下文菜单从ListViewItem获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆