我有以下ListBox:

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

定义为SequencesFilesItemsSourceObservableCollection<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);
}

是否可以双击而不是单击来调用Command(PlaylistLoadCommand)?

最佳答案

您可以将InputBinding设置为Button以双击触发命令

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

关于c# - 以编程方式绑定(bind)按钮以双击命令wpf,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36548413/

10-11 22:36