ListViewItem Context Menu Get Data from ListViewItem

旧时模样 提交于 2021-01-29 20:41:58

问题


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>

回答1:


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



来源:https://stackoverflow.com/questions/8786931/listviewitem-context-menu-get-data-from-listviewitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!