处理SelectionChanged和MouseDown

时间:2018-05-25 15:19:00

标签: c# wpf events mvvm

我有一个使用MVVM的WPF应用程序,该应用程序包含2个ListBoxes。由于我正在使用MVVM,我在我的XAML中使用EventTriggers,如下所示:

<ListBox x:Name="ListBox1" 
                 Grid.Row="0"
             ItemsSource="{Binding EventLogs, UpdateSourceTrigger=PropertyChanged}"
             SelectedItem="{Binding SelectedLocalLog, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
             ScrollViewer.HorizontalScrollBarVisibility="Hidden"
             ScrollViewer.VerticalScrollBarVisibility="Auto"
             SelectionMode="Single">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding LoadEventLogEntriesCommand}" CommandParameter="{Binding ElementName=ListBox1, Path=SelectedItem}" />
                </i:EventTrigger>
                <i:EventTrigger EventName="MouseDown">
                    <i:InvokeCommandAction Command="{Binding LoadEventLogEntriesCommand}" CommandParameter="{Binding ElementName=ListBox1, Path=SelectedItem}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
 </ListBox>

我的ViewModel中的代码:

this.LoadEventLogEntriesCommand = new DelegateCommand(this.LoadLog);

private void LoadLog()
{
        this.worker = new BackgroundWorker();
        this.worker.ProgressChanged += new ProgressChangedEventHandler(this.UpdateProgress);
        this.worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.RunWorkerCompleted);
        this.worker.WorkerReportsProgress = true;
        this.worker.WorkerSupportsCancellation = true;
        this.worker.DoWork += new DoWorkEventHandler(this.ReadLog);

        // read entries
        if (worker.IsBusy != true)
        {
            worker.RunWorkerAsync(this);
        }
}

当我点击ListBox中的一行时,我会触发事件SelectionChanged,因此在我的ViewModel中调用我的LoadLog()方法创建一个BackgroundWorker来做一些事情。但是,我知道这也会调用我的MouseDown事件,因此我将此方法调用了两次,因为我将ViewTriggers绑定到ViewModel中的同一个Command。

我想要的是之后我已经点击了ListBox中的一行我想再次点击同一行 并触发和事件来运行我的命令。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

您可以尝试处理MouseUp事件(仅限):

<i:EventTrigger EventName="MouseUp">
    <i:InvokeCommandAction Command="{Binding LoadEventLogEntriesCommand}" CommandParameter="{Binding ElementName=ListBox1, Path=SelectedItem}" />
</i:EventTrigger>
相关问题