繁体   English   中英

C#鼠标左键单击列表框

[英]c# mouse left click on listbox

当鼠标左键单击列表框中的项目时,如何执行功能? 我不能使用SelectChanged,因为我也要右键单击,并且右键单击该项目时,还要执行功能SelectChanged。

或如何在SelectChange方法中进行检测(如果事件是单击鼠标右键或鼠标左键)

WPF:

XAML:安装System.Windows.Interactivity.WPF nuget库

<Window x:Class="WpfApp1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:test="clr-namespace:Test"
        mc:Ignorable="d"
        Title="Window1">
    <Window.DataContext>
        <test:MainViewModel/>
    </Window.DataContext>
    <Grid>
        <ListBox ItemsSource="{Binding ListBoxItems}" HorizontalAlignment="Left" Width="216" Height="235" Margin="10,10,0,0" VerticalAlignment="Top">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseLeftButtonUp" >
                    <i:InvokeCommandAction Command="{Binding ListBoxLeftClickCommand}" CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </ListBox>
    </Grid>
</Window>

将MVVMLight库用于RelayCommand和ViewModelBase的ViewModel.cs

using System;
using System.Collections.ObjectModel;
using System.Windows.Input;

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;

namespace Test
{
    public class MainViewModel : ViewModelBase
    {
        public class TestModel
        {
            public string Value { get; set; }

            public TestModel(string value)
            {
                this.Value = value;
            }
        }

        public ObservableCollection<string> ListBoxItems { get; set; }

        public ICommand ListBoxLeftClickCommand { get; }

        public MainViewModel()
        {
            ListBoxLeftClickCommand = new RelayCommand<object>(DoSomething, selectedItem => true);
            ListBoxItems = new ObservableCollection<string>() { "Test1", "Test2" };
        }

        private void DoSomething(object selectedItem)
        {
            throw new NotImplementedException();
        }
    }
}

WinForms:取自右键单击以在列表框中选择项目

this.ListBox.MouseUp += new System.Windows.Forms.MouseEventHandler(this.List_LeftClick);

private void List_LeftClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        int index = this.listBox.IndexFromPoint(e.Location);
        if (index != ListBox.NoMatches)
        {
            // Do something
        }
    }
}
listBoxG.AddHandler(UIElement.MouseLeftButtonUpEvent, new RoutedEventHandler(OnMouseLeftButtonUp_listBoxG), true);

public void OnMouseLeftButtonUp_listBoxG(Object sender, RoutedEventArgs e)
{
// something
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM