【问题标题】:WPF MVVM. Left DoubleClick on ListViewItemWPF MVVM。左双击 ListViewItem
【发布时间】:2021-11-25 01:59:07
【问题描述】:

我想要什么:如果我在 ListViewItem 上执行左双击,我想调用一个方法。

我有什么:

Xaml:

 <ListView ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
                      Name="StructListView"
                      Margin="15" 
                      Grid.Row="2" 
                      BorderThickness="0" 
                      SelectedItem="{Binding SelectedStructObject}" 
                      ItemsSource="{Binding StructObjects, Mode=TwoWay}"
                      HorizontalAlignment="Left"
                      HorizontalContentAlignment="Left">

                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseDoubleClick">
                        <i:InvokeCommandAction 
                    Command="{Binding Command}"
                    CommandParameter="{Binding ElementName=StructListView, Path=SelectedItem}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                ...

主视图模型:

public class MainViewModel : BaseViewModel
{
    private StructElement _selectedStructObject;

    public StructElement SelectedStructObject
    {
        get { return _selectedStructObject; }
        set
        {
            if (_selectedStructObject != value)
            {
                _selectedStructObject = value;
                OnPropertyChanged();
            }
        }
    }

    public RelayCommand Command { get; set; }

    public ObservableCollection<StructElement> StructObjects { get; set; }

    private StructElement _structElement;

    public StructElement StructObject
    {
        get { return _structElement; }
        set
        {
            if (_structElement != value)
            {
                _structElement = value;
                OnPropertyChanged();
            }
        }
    }


    public MainViewModel()
    {

        Command = new RelayCommand(o => { //Method; });
      
        StructObjects = new ObservableCollection<StructElement>();         

    }
}

中继命令:

public class RelayCommand : ICommand
{
    private readonly Action<object> execute;
    private readonly Predicate<object> canExecute;

    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
        {
            throw new ArgumentNullException("execute");
        }

        this.execute = execute;
        this.canExecute = canExecute;
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public bool CanExecute(object parameter)
    {
        if (canExecute == null)
        {
            return true;
        }

        return canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        execute(parameter);
    }

}

我知道,如果我使用事件,我可以使用类似的东西:

void OnMouseDoubleClick(Object sender, MouseButtonEventArgs e)
{
    if (e.ChangedButton == MouseButton.Left)
    {
    // Method
    }
}

但由于我使用的是 RelayCommand,我没有像 MouseButtonEventArgs 这样的事件吗? 我想过用 RelayCommand “触发” OnMouseDoubleClick?

我对编程和 WPF 非常了解,所以如果您有更多关于我应该阅读以了解解决方案的内容的信息,我将不胜感激。

谢谢。

【问题讨论】:

  • does this answer your question?您已经有一个 EventTrigger,它在 MouseDoubleClick 上调用名为 Command 的命令并将 ListView 的 SelectedItem 作为参数传递。您需要将 Command 实现为 ICommand(可能将名称更改为更贴切的名称)并让它在其 getter 中创建一个 RelayCommand,就像我链接的示例一样。

标签: c# wpf xaml mvvm


【解决方案1】:

我从链接中发现了一个不同的解决方案。

在我的 XAML 中,我使用 MVVMLight 中的“EventToCommand”:

                    <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseDoubleClick">
                        <cmd:EventToCommand Command="{Binding LightCommand}"
                                            PassEventArgsToCommand="True"/>                            
                    </i:EventTrigger>
                    </i:Interaction.Triggers>

我将 RelayCommand 设为通用,这样我就可以传递一个事件(我希望我的描述正确):

 public class RelayCommand : ICommand
{
    private readonly Action<object> execute;
    private readonly Predicate<object> canExecute;

    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
        {
            throw new ArgumentNullException("execute");
        }

        this.execute = execute;
        this.canExecute = canExecute;
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public bool CanExecute(object parameter)
    {
        if (canExecute == null)
        {
            return true;
        }

        return canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        execute(parameter);
    }

}

在我的 MainViewModel 中,我声明了一个带有 MouseButtonEventArgs 的 RelayCommand 并传递了一个方法来检查 MouseButton 是否为左键并执行某些操作:

public RelayCommand<MouseButtonEventArgs> LightCommand { get; set; }

public void dosomething(MouseButtonEventArgs e)
    {
        if (e.ChangedButton == MouseButton.Left) 
        {
            MessageBox.Show(e.OriginalSource.GetType().ToString());
        }           
    }


public MainViewModel()
    {       
        LightCommand = new RelayCommand<MouseButtonEventArgs>(dosomething);
    }

【讨论】:

    猜你喜欢
    • 2011-04-16
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多