2016-04-11 4 views
0

Je le suivant ListBox:bouton par programme se lient à double-clic commande WPF

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

Le SequencesFiles défini comme ItemsSource est un ObservableCollection<Button>.

Je suis d'ajouter manuellement nouvelle Buttons aux collections en utilisant la fonction suivante:

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

Est-il possible d'appeler la Command (PlaylistLoadCommand) sur double-cliquez et non sur un clic?

+0

* J'ajouter manuellement de nouveaux boutons aux collections ... * Eh bien, il y a votre problème. Vous pouvez probablement atteindre vos objectifs plus facilement. Vous pourriez vouloir examiner * pourquoi * vous faites cela, et peut-être demander une meilleure, plus mvvm/wpf-ish façon d'accomplir vos objectifs. Dans une autre question. – Will

+0

Pouvez-vous voter pour ma réponse (badges et autres)? –

Répondre

3

Vous pouvez mettre InputBinding à votre Button pour tirer votre commande double-cliquez

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