首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自CommandBinding的MVVM调用命令

来自CommandBinding的MVVM调用命令
EN

Stack Overflow用户
提问于 2015-06-19 00:28:47
回答 1查看 2.7K关注 0票数 7

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

我知道如何做到这一点的唯一方法是在后面的代码中这样做:

代码语言:javascript
复制
    <UserControl.CommandBindings>
            <CommandBinding Command="ApplicationCommands.Delete" Executed="OnDeleteCommand" />
    </UserControl.CommandBindings>

有任何MVVM实现同样的方法吗?

EN

回答 1

Stack Overflow用户

发布于 2015-06-19 07:03:01

以下是删除逻辑的示例:

代码语言:javascript
复制
<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>

这里有一个注意事项:

  • delete按钮必须到达DataContext的ViewModel,所以语法允许我们进入窗口的DataContext,这实际上正是我们想要的。
代码语言:javascript
复制
- For the CommandParameter we need the SelectedItem of the DataGrid so, using RelativeSource we are able to accomplish this. 

ViewModel:

代码语言:javascript
复制
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);
    }
}

型号:

代码语言:javascript
复制
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 { };
}

删除命令:

代码语言:javascript
复制
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);
    }
}

以及设置DataContext的代码隐藏:

代码语言:javascript
复制
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new ViewModel();
    }
}

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

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

代码语言:javascript
复制
public void Execute(object parameter)
{
   _vm.items.Remove(parameter as Model);
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30928076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档