vee
vee

Reputation: 429

Handling SelectionChanged and MouseDown

I have a WPF application using MVVM, the application contains 2 ListBoxes. Since I am using MVVM I am using EventTriggers in my XAML as seen below:

<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>

Code in my 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);
        }
}

When I click a Row in the ListBox, I trigger the Event SelectionChanged and therefore call my LoadLog() Method in my ViewModel that creates a BackgroundWorker to do some stuff. However, I understand that this also calls my MouseDown event and therefore this method is being called twice since I have the EventTriggers binded to the same Command in my ViewModel.

What I want is after I have already clicked a row in the ListBox I want to click the same row again and trigger and event to run my command. How can I do this?

Upvotes: 0

Views: 433

Answers (1)

mm8
mm8

Reputation: 169420

You could try to handle the MouseUp event (only):

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

Upvotes: 1

Related Questions