如何右键点击项目从Control和WPF打开的菜单 [英] how to right click on item from Listbox and open menu on WPF

查看:173
本文介绍了如何右键点击项目从Control和WPF打开的菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有列表框,在文件中,我希望能够右键打开等,以便从列表框中删除文件删除的菜单。



目前,我有该功能后,右键单击项目我的列表框

 私人无效listBoxFiles_PreviewMouseRightButtonDown(对象发件人,MouseButtonEventArgs E)
{$内b
$ b}

在XAML我实现右键点击后删除菜单。

 < ListBox.ContextMenu> 
<&文本菜单GT;
<菜单项标题=删除/>
< /文本菜单>
< /ListBox.ContextMenu>



谁从我的列表框删除文件中的函数:

 私人无效MenuItemDelete_Click(对象发件人,RoutedEventArgs E)
{
如果(listBoxFiles.SelectedIndex == -1)
{
返回;
}

//字符串文件路径=(listBoxFiles.SelectedItem)的ToString();
INT指数= listBoxFiles.SelectedIndex;
listBoxFiles.Items.RemoveAt(指数);
}


解决方案

您已经有一个上下文菜单与您的标记。



如果您要执行一些操作,方式之一是检查被点击菜单的Click函数项。
为例,你的下一个列表框:

 <列表框名称=someListBox> 
< ListBox.ContextMenu>
<&文本菜单GT;
<菜单项标题=删除点击=MenuItemDelete_Click/>
< /文本菜单>
< /ListBox.ContextMenu>

<&ListBoxItem的GT; ...< / ListBoxItem的>
<&ListBoxItem的GT; ...< / ListBoxItem的>
<&ListBoxItem的GT; ...< / ListBoxItem的>

< /列表框>

和功能可能是下一个:

 私人无效MenuItemDelete_Click(对象发件人,RoutedEventArgs E)
{
如果(someListBox.SelectedIndex == -1)回报;

//假设功能GetElement获取某个元素
VAR元= GetElement(someListBox.SelectedIndex);

//假设功能DeleteElement
DeleteElement(元);
}



更新2 2012年3月:



下面是你的列表框的另一种变体。您可以添加上下文菜单不列表框,但到列表框中的项目。例如:

 <列表框名称=someListBox的MouseDown =someListBox_MouseDown> 
< ListBox.Resources>

<! - 定义的上下文菜单 - >
<文本菜单X:键=MyElementMenu>
<菜单项标题=删除点击=MenuItemDelete_Click/>
< /文本菜单>

<! - 设置每个ListBoxItem的当前列表框的上下文菜单 - >
<风格的TargetType ={X:输入一个ListBoxItem}>
< setter属性=文本菜单VALUE ={StaticResource的MyElementMenu}/>
< /样式和GT;

< /ListBox.Resources>
<&ListBoxItem的GT; ...< / ListBoxItem的>
<&ListBoxItem的GT; ...< / ListBoxItem的>
<&ListBoxItem的GT; ...< / ListBoxItem的>
< /列表框>



1),此功能将unsellect的所有项目,当你点击在列表框中的空白:

 私人无效someListBox_MouseDown(对象发件人,MouseButtonEventArgs E)
{
someListBox.UnselectAll();
}



2)当你点击lisboxt项目,它是蓝色的。当你右键点击列表框项目,它仍然是蓝色的,但如果出现一个上下文菜单,列表框项变为灰色,也许是因为这个项目失去焦点。



3)删除功能正常工作:

 私人无效MenuItemDelete_Click(对象发件人,RoutedEventArgs E)
{
如果(someListBox.SelectedIndex == -1)
{
的回报;
}

someListBox.Items.RemoveAt(someListBox.SelectedIndex);
}


i have Listbox with files in, i want to able to right click and open a menu like Delete in order to remove files from the Listbox.

currently i have this function after right click on item inside my Listbox

private void listBoxFiles_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{

}

and i implement at XAML Delete menu after right click

          <ListBox.ContextMenu>
                <ContextMenu>                                                        
                    <MenuItem Header="Delete"/>
                </ContextMenu>
            </ListBox.ContextMenu>

the function who delete file from my ListBox:

private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
{            
    if (listBoxFiles.SelectedIndex == -1)
    {
        return;
    }

    //string filePath = (listBoxFiles.SelectedItem).ToString();
    int index = listBoxFiles.SelectedIndex;
    listBoxFiles.Items.RemoveAt(index);
}

解决方案

You already have a context menu with your markup.

If you want to perform some operation, one of the ways is to check which item was clicked in the menu's Click function. For example, you have the next listbox:

<ListBox Name="someListBox">
    <ListBox.ContextMenu>
         <ContextMenu>
             <MenuItem Header="Delete" Click="MenuItemDelete_Click"/>
         </ContextMenu>
    </ListBox.ContextMenu>

    <ListBoxItem>...</ListBoxItem>
    <ListBoxItem>...</ListBoxItem>
    <ListBoxItem>...</ListBoxItem>

</ListBox>

And function may be next:

private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
{
    if (someListBox.SelectedIndex == -1) return;

    // Hypothetical function GetElement retrieves some element
    var element = GetElement(someListBox.SelectedIndex);

    // Hypothetical function DeleteElement
    DeleteElement(element);
}

Updated 5 March 2012:

Here is another variant of your listbox. You can add a context menu not to listbox but to the listbox items. For example:

<ListBox Name="someListBox" MouseDown="someListBox_MouseDown">
    <ListBox.Resources>

        <!--Defines a context menu-->
        <ContextMenu x:Key="MyElementMenu">
            <MenuItem Header="Delete" Click="MenuItemDelete_Click"/>
        </ContextMenu>

        <!--Sets a context menu for each ListBoxItem in the current ListBox-->
        <Style TargetType="{x:Type ListBoxItem}">
             <Setter Property="ContextMenu" Value="{StaticResource MyElementMenu}"/>
        </Style>

    </ListBox.Resources>
    <ListBoxItem>...</ListBoxItem>
    <ListBoxItem>...</ListBoxItem>
    <ListBoxItem>...</ListBoxItem>
</ListBox>

1) This function will unsellect all items when you clicked on the empty space in the listbox:

private void someListBox_MouseDown(object sender, MouseButtonEventArgs e)
{
    someListBox.UnselectAll();
}

2) When you click the lisboxt item, it is blue. When you right click the listbox item, it is still blue, but if a context menu appears, the listbox item becomes gray, maybe it is so because this item loses a focus.

3) Delete function works fine:

private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
{
    if (someListBox.SelectedIndex == -1)
    {
        return;
    }

    someListBox.Items.RemoveAt(someListBox.SelectedIndex);
}

这篇关于如何右键点击项目从Control和WPF打开的菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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