2016-04-11 59 views
0

我有以下ListBox編程綁定按鈕雙擊命令WPF

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

SequencesFiles定義爲ItemsSourceObservableCollection<Button>

我手動添加新Buttons使用下面的函數集合:

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

是否可以調用CommandPlaylistLoadCommand)後雙擊,而不是在點擊?

+0

解僱你的命令*我手動添加新的按鈕到收藏... *嗯,有你的問題。你可以用更簡單的方式實現你的目標。你可能想要回顧*爲什麼*你這樣做,也許要求一個更好,更mvvm/wpf-ish的方式來完成你的目標。在另一個問題。 – Will

+0

你可以提高我的答案(徽章和東西)嗎? –

回答

3

你可以設置InputBindingButton上雙擊

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