0

I have the following ListBox:

<ListBox x:Name="SequencesFilesListBox" ItemsSource="{Binding SequencesFiles, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Foreground="DarkBlue" BorderBrush="Transparent" />

The SequencesFiles defined as ItemsSource is an ObservableCollection<Button>.

I'm manually adding new Buttons to the collections using the following function:

private void AddSequenceToPlaylist(string currentSequence)
{
    if (SequencesFiles.Any(currentFile => currentFile.ToolTip == currentSequence)) return; 

    var newSequence = new Button
    {
        ToolTip = currentSequence,
        Background = Brushes.Transparent,
        BorderThickness = new Thickness(0),
        HorizontalAlignment = HorizontalAlignment.Stretch,
        HorizontalContentAlignment = HorizontalAlignment.Stretch,
        Content = Path.GetFileName(currentSequence),
        Command = PlaylistLoadCommand,
        CommandParameter = currentSequence,
    };
    SequencesFiles.Add(newSequence);
}

Is it possible to call the Command (PlaylistLoadCommand) upon double-click and not upon click?

Idanis
  • 1,710
  • 6
  • 32
  • 63
  • *I'm manually adding new Buttons to the collections...* Well, there's your problem. You can probably achieve your goals in an easier way. You might want to review *why* you are doing this, and perhaps ask for a better, more mvvm/wpf-ish way of accomplishing your goals. In another question. –  Apr 11 '16 at 17:05
  • Can you up-vote my answer( badges and stuff)? – Noam M Apr 17 '16 at 13:04

2 Answers2

3

You can set InputBinding to your Button to fire your command on double click

var newSequence = new Button
{
    ToolTip = currentSequence,
    Background = Brushes.Transparent,
    BorderThickness = new Thickness(0),
    HorizontalAlignment = HorizontalAlignment.Stretch,
    HorizontalContentAlignment = HorizontalAlignment.Stretch,
    Content = Path.GetFileName(currentSequence),
    CommandParameter = currentSequence,
};

var mouseBinding = new MouseBinding();
mouseBinding.Gesture = new MouseGesture(MouseAction.LeftDoubleClick);
mouseBinding.Command = PlaylistLoadCommand;
newSequence.InputBindings.Add(mouseBinding);
cweston
  • 10,371
  • 17
  • 74
  • 104
Nitin
  • 17,418
  • 2
  • 33
  • 51
0

Like in this question, I would advise against creating User Controls in your ViewModel : how to correctly bind a View control to a ViewModel List (WPF MVVM)

For double click bindings, unfortunately it's still not supported in the WPF toolkit see this question: How to bind a command in WPF to a double click event handler of a control?

Community
  • 1
  • 1
Petr Vávro
  • 1,476
  • 1
  • 8
  • 12