简体   繁体   English

如何将应用程序命令绑定到视图模型(WPF)?

[英]How to bind application commands to view model(WPF)?

I have already read Josh Smiths article about binding commands to view model using RelayCommand. 我已经阅读了Josh Smiths关于使用RelayCommand查看模型的绑定命令的文章。 However I need to bind ApplicationCommands.Save to a view model so that when a user clicks the save menu item it is handled in the window. 但是,我需要将ApplicationCommands.Save绑定到视图模型,以便当用户单击保存菜单项时,它将在窗口中处理。 How is it possible? 这怎么可能?

There is a good tutorial to help bind application commands. 有一个很好的教程可以帮助绑定应用程序命令。

  1. Setup your command binding collection for your view to bind to in your view model. 为视图设置命令绑定集合以在视图模型中绑定。 eg: 例如:
 public CommandBindingCollection CommandBindings { get; } public YourViewModel() { //Create a command binding for the save command var saveBinding = new CommandBinding(ApplicationCommands.Save, SaveExecuted, SaveCanExecute); //Register the binding to the class CommandManager.RegisterClassCommandBinding(typeof(YourViewModel), saveBinding); //Adds the binding to the CommandBindingCollection CommandBindings.Add(saveBinding); } 
  1. Setup attached properties. 设置附加属性。 Read the article on how to do that. 阅读有关如何做到这一点的文章

  2. Then bind to it using attached properties. 然后使用附加属性绑定到它。

 <UserControl local:AttachedProperties.RegisterCommandBindings="{Binding CommandBindings}"> <Window.DataContext> <local:YourViewModel></local:YourViewModel> </Window.DataContext> </UserControl> 

The best solution I'm aware of us to use a service. 我知道我们使用服务的最佳解决方案。 For example, an ICommandBindingsProvider like this: 例如,像这样的ICommandBindingsProvider:

public interface ICommandBindingsProvider
{
    CommandBindingCollection CommandBindings { get; }
}

This gets injected into your ViewModel and used like this: 这会被注入您的ViewModel并像这样使用:

public MyViewModel(ICommandBindingsProvider commandBindings)
{
    commandBindings.Add(new CommandBinding(....));
}

How the service gets injected will be dependent on what MVVM framework you're using. 如何注入服务将取决于您正在使用的MVVM框架。 Most (but not all) support some sort of service injection capabilities. 大多数(但不是全部)支持某种服务注入功能。 If you need a more concrete code example, look at Onyx which does service injection and has a service that does just this. 如果您需要一个更具体的代码示例,请查看Onyx ,它执行服务注入并具有执行此操作的服务。

How is your save menu item being created? 你的保存菜单项是如何创建的? Somewhere it must be getting bound to the ApplicationCommands.Save? 它某处必须绑定到ApplicationCommands.Save? You can change that so that it binds to your ViewModel's save instead. 您可以更改它,以便它与ViewModel的保存绑定。

Or, you place the save command binding on the viewmodel, and then databind to it, something like (pseudo code) 或者,将save命令绑定放在viewmodel上,然后将数据绑定到它,类似于(伪代码)

in the viewmodel: 在viewmodel中:

...
public Command SaveCommand { get { return yourCodeHere; } }
...

in the view: 在视图中:

<Button Command="{Binding Path=SaveCommand}" Content="Click"/>    

Update: i just tried binding to a command in a commandbinding and it doesn't work. 更新:我刚刚尝试绑定到命令绑定中的命令,但它不起作用。 what i was thinking of was binding to a command . 我在想的是绑定命令

I have used this approach successfully in the past: 我过去成功使用过这种方法:

Start out by defining a CommandBindings property on ViewModels (same definition as on Views). 首先在ViewModels上定义一个CommandBindings属性(与Views相同的定义)。

Then for each Command implemented by ViewModel, add a CommandBinding to ViewModel's CommandBindings. 然后,对于ViewModel实现的每个Command,将CommandBinding添加到ViewModel的CommandBindings。

Then, when you set: 然后,当你设置:

view.DataSource = viewModel;

Also set: 还设置:

view.CommandBindings.AddRange(viewModel.CommandBindings);

I think may be done using a binding of the CommandBindings property (can't remember for sure). 我想可以使用CommandBindings属性的绑定来完成(无法确定)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM