来自 CommandBinding 的 MVVM 调用命令

mFe*_*ein 6 data-binding wpf mvvm icommand

我想将 a 绑定CommandBinding到 ViewModel ICommand,这样,当用户点击时,Delete我可以触发我的 ViewModel 的删除逻辑。

我知道怎么做的唯一方法是在后面的代码中,用这个:

    <UserControl.CommandBindings>
            <CommandBinding Command="ApplicationCommands.Delete" Executed="OnDeleteCommand" />
    </UserControl.CommandBindings>
Run Code Online (Sandbox Code Playgroud)

任何实现相同目标的 MVVM 方法?

Ola*_*cea 0

这是删除逻辑的示例:

<Window x:Class="DeleteCommandStack.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <DataGrid ItemsSource="{Binding items}" AutoGenerateColumns="False" CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Name}"/>
                            <Button Content="Delete" 
                                    Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.deleteCommand}" 
                                    CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=SelectedItem}"/>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>
Run Code Online (Sandbox Code Playgroud)

这里需要注意一点:

  • 删除按钮必须到达 ViewModel 的 DataContext,因此该语法让我们可以到达 Window 的 DataContext,这实际上正是我们想要的。

    • 对于CommandParameter,我们需要DataGrid 的SelectedItem,因此,使用RelativeSource 我们能够完成此任务。

视图模型:

public class ViewModel
{
    public ObservableCollection<Model> items { get; set; }

    public ICommand deleteCommand { get; set; }

    public ViewModel()
    {
        items = new ObservableCollection<Model>();
        items.Add(new Model() { Name = "Name1" });
        items.Add(new Model() { Name = "Name2" });
        items.Add(new Model() { Name = "Name3" });
        items.Add(new Model() { Name = "Name4" });

        deleteCommand = new DeleteCommand(this);
    }

    public void DeleteHandler(object parameter)
    {
        items.Remove(parameter as Model);
    }
}
Run Code Online (Sandbox Code Playgroud)

模型:

public class Model : INotifyPropertyChanged
{
    private string _Name;

    public string Name
    {
        get { return _Name; }
        set
        {
            _Name = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Name"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}
Run Code Online (Sandbox Code Playgroud)

删除命令:

public class DeleteCommand : ICommand
{
    private ViewModel _vm;
    public DeleteCommand(ViewModel vm)
    {
        _vm = vm;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        _vm.DeleteHandler(parameter);
    }
}
Run Code Online (Sandbox Code Playgroud)

以及设置 DataContext 的代码隐藏:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new ViewModel();
    }
}
Run Code Online (Sandbox Code Playgroud)

通过将 ViewModel 引用发送到 DeleteCommand,我们可以调用它们的方法并将参数发送到它们的方法。

我们可以选择直接从命令中删除该项目:

public void Execute(object parameter)
{
   _vm.items.Remove(parameter as Model);
}
Run Code Online (Sandbox Code Playgroud)

我想就是这样,你现在有了一个有效的例子。