【发布时间】:2019-08-06 14:39:20
【问题描述】:
我正在使用 MVVM 方法实现一个列表框,但我遇到了一个问题,即我的双击命令没有被触发。当我启动我的应用程序时,我看到我的 ListBox 通过绑定填充得很好。但是当我双击一个项目时,什么也没有发生。有什么我想念的吗?提前谢谢了。
这是我的 UserControl (xaml) 设置方式
<ListBox
x:Name="Files"
ItemsSource="{Binding FileCollection, Mode=TwoWay}"
SelectedItem="{Binding Filename, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}">
<TextBlock.InputBindings>
<MouseBinding
Gesture="LeftDoubleClick"
Command="{Binding EditFileCommand}"/>
</TextBlock.InputBindings>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这就是我在 View Model 中设置命令对象的方式:
//..using system.windows.input for ICommand
private ICommand editFileCommand = null;
public ICommand EditFileCommand
{
get
{
//RelayCommand comes from GalaSoft.MvvmLight.Command
return editFileCommand ?? new RelayCommand(EditFile);
}
}
private void EditFile()
{
MessageBox.Show("Double Click!");
}
【问题讨论】:
标签: c# wpf data-binding