1

按下删除键不会导致下面的命令触发。可以假设 DelegateCommand 遵循接口契约,因为 View 中引用的其他 DelegateCommand 实例在 ViewModel 中正确触发。

为什么?

看法:

<ListBox Height="132"
     Name="lbFiles"
     ItemsSource="{Binding LbItems, UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"  
     VerticalAlignment="Center"
     SelectedValue="{Binding Path=ListBoxSelection}">
<ListBox.InputBindings>
    <KeyBinding Key="Delete"
                Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}
                ,Path=DataContext.CommandInputFilesDeleteSelected}" />
</ListBox.InputBindings>

视图模型:

private DelegateCommand commandInputFilesDeleteSelected;

public ICommand CommandInputFilesDeleteSelected {
    get {
            if (commandInputFilesDeleteSelected == null) {
                commandInputFilesDeleteSelected = new DelegateCommand(InputFilesDeleteSelected);
            }
    return saveCommand;
    }
}

private void InputFilesDeleteSelected() { //this never fires :( 
        LbItems.Remove(ListBoxSelection); 
}
4

1 回答 1

0
private DelegateCommand commandInputFilesDeleteSelected;

public ICommand CommandInputFilesDeleteSelected {
    get {
            if (commandInputFilesDeleteSelected == null) {
                commandInputFilesDeleteSelected = new DelegateCommand(InputFilesDeleteSelected);
            }
    return saveCommand;  //changed from saveCommand to commandInputFilesDeleteSelected whoops
    }
}
于 2013-01-30T21:05:04.177 回答