右键单击后不显示菜单

时间:2014-06-11 21:35:42

标签: c# wpf

目标:
当您右键单击空白区域时,右侧菜单不会显示。第二张图是错的。

问题:
我应该如何做到这一点?我不知道正确的语法代码。

        private void lvw_bokade_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            ListView selectedData = (ListView)sender;

            List myList= (List) selectedData.SelectedItem;

            ContextMenu myContextMenu = new ContextMenu(); ;

            if (MouseButtonState.Released == e.RightButton && myList.Test!= "")
            {
                MenuItem menuItem3 = new MenuItem();
                menuItem3.Header = "Add quantity";
                myContextMenu.Items.Add(menuItem3);

                MenuItem menuItem2 = new MenuItem();
                menuItem2.Header = "Delete";
                myContextMenu.Items.Add(menuItem2);
            }

            myContextMenu.IsOpen = true;
        }

XAML代码:

    <TabItem Header="Bokade">
        <Grid Background="#FFE5E5E5">
            <ListView x:Name="lvw_bokade" HorizontalAlignment="Left" ItemsSource="{Binding}" SelectionMode="Single" Height="335" Margin="10,46,0,0" VerticalAlignment="Top" Width="561" MouseRightButtonUp="lvw_bokade_MouseRightButtonUp">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Fornamn" Width="auto" DisplayMemberBinding="{Binding Path=Fornamn }" TextBlock.TextAlignment="Left" />
                        <GridViewColumn Header="Efternamn" Width="auto" DisplayMemberBinding="{Binding Path=Efternamn}" TextBlock.TextAlignment="Left"  />
                        <GridViewColumn Header="Kortnummer" Width="auto" DisplayMemberBinding="{Binding Path=Kortnummer}" TextBlock.TextAlignment="Center"  />
                        <GridViewColumn Header="Personnummer" Width="auto" DisplayMemberBinding="{Binding Path=Personnummer}" TextBlock.TextAlignment="Center"  />
                        <GridViewColumn Header="Avprickad" Width="auto" DisplayMemberBinding="{Binding Path=Avprickad }" TextBlock.TextAlignment="Center"  />
                    </GridView>
                </ListView.View>
            </ListView>
            <Button Content="Ny plats" HorizontalAlignment="Left" Margin="10,21,0,0" VerticalAlignment="Top" Width="75"/>
        </Grid>
    </TabItem>

enter image description here

2 个答案:

答案 0 :(得分:0)

使用ListView.ItemContainerStyle设置ListViewItem的ContextMenu,如下所示。您无需订阅MouseRightButtonUp事件。     

    <ListView.Resources>
        <!-- Setup the context menu -->
        <ContextMenu x:Key="ItemContextMenu">
            <!-- Add menu items to the context menu -->
            <MenuItem Header="Add quantity" Command="{Binding Path=DataContext.AddQuantityCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}" />
            <MenuItem Header="Delete" Command="{Binding Path=DataContext.DeleteQuantityCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}" />
            <MenuItem Header="More Details." />
        </ContextMenu>
    </ListView.Resources>

    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <!-- Set the context menu for the ListViewItem to be the ItemContextMenu-->
            <Setter Property="ContextMenu" Value="{StaticResource ItemContextMenu}" />
        </Style>
    </ListView.ItemContainerStyle>

    <ListView.View>
        <GridView>
            <GridViewColumn Width="140" Header="OrderId" DisplayMemberBinding="{Binding Path=OrderId}" />
            <GridViewColumn Width="140" Header="Name"  DisplayMemberBinding="{Binding Path=Name}" />
            <GridViewColumn Width="140" Header="Quantity" DisplayMemberBinding="{Binding Path=Quantity}" />
        </GridView>
    </ListView.View>

</ListView>

答案 1 :(得分:0)

private void lvw_bokade_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
            ListView selectedData = (ListView)sender;

            Passlista myPasslista = (Passlista)selectedData.SelectedItem;

            System.Windows.Point pt = e.GetPosition(this);

            if (myPasslista != null && !(System.Windows.Media.VisualTreeHelper.HitTest(this, pt).VisualHit is ScrollViewer))
            {
                ContextMenu myContextMenu = new ContextMenu(); ;

                if (MouseButtonState.Released == e.RightButton && myPasslista.Instruktor != "")
                {
                    MenuItem menuItem3 = new MenuItem();
                    menuItem3.Header = "Add quantity";
                    myContextMenu.Items.Add(menuItem3);

                    MenuItem menuItem2 = new MenuItem();
                    menuItem2.Header = "Delete";
                    myContextMenu.Items.Add(menuItem2);
                }

                myContextMenu.IsOpen = true;
            }
}