Idanis
Idanis

Reputation: 1988

programmatically bind button to double click command wpf

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?

Upvotes: 0

Views: 1327

Answers (2)

Nitin Purohit
Nitin Purohit

Reputation: 18578

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

Upvotes: 3

Petr V&#225;vro
Petr V&#225;vro

Reputation: 1516

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?

Upvotes: 0

Related Questions