none
wpf如何给样式中的右键菜单设置事件? RRS feed

  • 问题

  • <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="true">
                        <Setter Property="ContextMenu" >
                            <Setter.Value>
                                <ContextMenu x:Name="ItemViewRightMenu">
                                     <MenuItem Header="确认"    >
                                    </MenuItem>
                                 </ContextMenu>
                              </Setter.Value>
                        </Setter>

    </Trigger>

                </Style.Triggers>

    注意一下,这个右键菜单是在依附于ListViewItem控件的,不能直接在MenuItem里面添加事件,所以 <EventSetter Event="Click" Handler="MenuItem_DoubleClick"  /> 放在哪里好?


    2016年8月29日 9:50

答案

  • 您好 轮回的齿轮,

    从你的描述中,我发现,你想在选中 ListViewItem 时,可以右击并能触发事件。 我做了一个小示例,代码与你之前稍微差别,但是我觉得可以很好的实现你想要的功能。下面的代码供你参考。

    XAML:

    <Grid>
        <ListView x:Name="textListView">
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <EventSetter Event="PreviewMouseRightButtonDown" Handler="OnListViewItemPreviewMouseRightButtonDown" />
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="右键事件1" Click="ContextMenuItem1Clicked"></MenuItem>
                    <MenuItem Header="右键事件2"  Click="ContextMenuItem2Clicked"></MenuItem>
                </ContextMenu>
            </ListView.ContextMenu>
            <ListViewItem>项1</ListViewItem>
            <ListViewItem>项2</ListViewItem>
            <ListViewItem>项3</ListViewItem>
            <ListViewItem>项4</ListViewItem>
        </ListView>
    </Grid>

    C# code:

    public partial class ListViewItemEvent : Window
    {
        public ListViewItemEvent()
        {
            InitializeComponent();
        }
    
        private void OnListViewItemPreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            //点击选中项之前事件    
            e.Handled = true;
        }
    
        private void ContextMenuItem1Clicked(object sender, RoutedEventArgs e)
        {
            //获取选中的项
            ListViewItem selected_lvi = this.textListView.SelectedItem as ListViewItem;
        }
    
        private void ContextMenuItem2Clicked(object sender, RoutedEventArgs e)
        {
            //获取选中的项
            ListViewItem selected_lvi = this.textListView.SelectedItem as ListViewItem;
        }
    }
    
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年8月30日 14:23

全部回复

  • 您好 轮回的齿轮,

    从你的描述中,我发现,你想在选中 ListViewItem 时,可以右击并能触发事件。 我做了一个小示例,代码与你之前稍微差别,但是我觉得可以很好的实现你想要的功能。下面的代码供你参考。

    XAML:

    <Grid>
        <ListView x:Name="textListView">
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <EventSetter Event="PreviewMouseRightButtonDown" Handler="OnListViewItemPreviewMouseRightButtonDown" />
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="右键事件1" Click="ContextMenuItem1Clicked"></MenuItem>
                    <MenuItem Header="右键事件2"  Click="ContextMenuItem2Clicked"></MenuItem>
                </ContextMenu>
            </ListView.ContextMenu>
            <ListViewItem>项1</ListViewItem>
            <ListViewItem>项2</ListViewItem>
            <ListViewItem>项3</ListViewItem>
            <ListViewItem>项4</ListViewItem>
        </ListView>
    </Grid>

    C# code:

    public partial class ListViewItemEvent : Window
    {
        public ListViewItemEvent()
        {
            InitializeComponent();
        }
    
        private void OnListViewItemPreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            //点击选中项之前事件    
            e.Handled = true;
        }
    
        private void ContextMenuItem1Clicked(object sender, RoutedEventArgs e)
        {
            //获取选中的项
            ListViewItem selected_lvi = this.textListView.SelectedItem as ListViewItem;
        }
    
        private void ContextMenuItem2Clicked(object sender, RoutedEventArgs e)
        {
            //获取选中的项
            ListViewItem selected_lvi = this.textListView.SelectedItem as ListViewItem;
        }
    }
    
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年8月30日 14:23
  • 能不能再加一个判断,选中listviewitem,如果右击的是listview的空白处而不是listviewitem,就弹出不同的右键菜单或者隐藏右键菜单?能不能做到?若能的话应用到treeviewitem可不可以?
    2016年9月2日 12:53
  • 您好 轮回的锯齿,

    >>"能不能再加一个判断,选中listviewitem,如果右击的是listview的空白处而不是listviewitem,就弹出不同的右键菜单或者隐藏右键菜单?"

    是可以做到的,不过您需要为ListView和ListViewItem设置不同的ContextMenu. 这样的方法同样适用于TreeView.

    <ListView>
        <ListView.ContextMenu>
            <ContextMenu>
                <MenuItem Header="menu item in listview"/>
            </ContextMenu>
        </ListView.ContextMenu>
        <ListViewItem Content="ListView item 1">
            <ListViewItem.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="menu item in listview item"/>
                </ContextMenu>
            </ListViewItem.ContextMenu>
        </ListViewItem>
    </ListView>
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年10月7日 2:18