我有以下ListBox
:
<ListBox x:Name="SequencesFilesListBox" ItemsSource="{Binding SequencesFiles, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Foreground="DarkBlue" BorderBrush="Transparent" />
定义为
SequencesFiles
的ItemsSource
是ObservableCollection<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/