简体   繁体   English

WPF代码隐藏等效

[英]WPF Code-behind equivalent

When developing, I like to try and understand more than "just do this". 在开发时,我喜欢尝试理解的不仅仅是“只是这样做”。 Especially with WPF, I like to understand BOTH aspects of the bindings... both from the GUI (xaml) and from the code-behind. 特别是对于WPF,我喜欢从GUI(xaml)和代码隐藏中理解绑定的两个方面。 That being said, I am wondering what the code equivalent would be for the following. 话虽这么说,我想知道以下代码的等价物。

I have a ViewModel with some pre-defined "ICommand" instances, such as add, edit, save, cancel, exit, whatever -- and they work as expected. 我有一个带有一些预定义的“ICommand”实例的ViewModel,例如添加,编辑,保存,取消,退出等等 - 它们按预期工作。 Now, looking at the binding of the View (Window) that has a button on it, I have it's binding to the commands, something like. 现在,看看有一个按钮的View(Window)的绑定,我将它绑定到命令,类似于。

<Button Command="{Binding ExitCommand}" Content="Exit" ... />

and this properly does what I expect for allowing the form to exit (and do whatever else I'm playing with). 这恰当地完成了我希望允许表单退出(并做我正在玩的其他任何事情)。

What would the code-behind look like for this. 代码隐藏的后果是什么样的。 I know that with properties, such as IsEnabled or IsVisible are bound to dependency object / properties, but I don't understand the correlation when binding to a command's execution. 我知道对于属性,例如IsEnabled或IsVisible绑定到依赖对象/属性,但是我不理解绑定到命令执行时的相关性。 Thanks. 谢谢。

You create the Command Binding the same way you would any other binding in code behind. 您创建命令绑定的方式与后面的代码中的任何其他绑定相同。

For example, 例如,

Binding b = new Binding();
b.Source = myViewModel;
b.Path = new PropertyPath("ExitCommand");
MyButton.SetBinding(Button.CommandProperty, b);

Command bindings expect to be bound to an object of type ICommand . 命令绑定期望绑定到ICommand类型的对象。 When they get executed, such as on a Button Click, they first call ICommand.CanExecute() and if that is true then they call ICommand.Execute() . 当它们被执行时,例如按钮单击,它们首先调用ICommand.CanExecute() ,如果是,则它们调用ICommand.Execute() If the CommandParameter property is set then that is used when evaluating CanExecute and Execute 如果设置了CommandParameter属性,则在评估CanExecuteExecute时使用该属性

With WPF Buttons that have a Command binding, the IsEnabled property is automatically bound to the result of ICommand.CanExecute . 对于具有Command绑定的WPF按钮, IsEnabled属性自动绑定到ICommand.CanExecute的结果。 The CanExecute method is run once when the button is first loaded, and run again anytime the Command Binding changes. 首次加载按钮时, CanExecute方法运行一次,并在命令绑定更改时随时再次运行。

If you want it to update more frequently, such as when the CommandParameter changes, you need to hook up something extra to update the binding when the CommandParameter changes. 如果您希望更频繁地更新,例如CommandParameter更改时,您需要连接一些额外的东西以在CommandParameter更改时更新绑定。 Most RelayCommands I see have this built-in, such as MVVM Light's RelayCommand , althought other commands such as Microsoft PRISM's DelegateCommand do not have this behavior by default. 我看到的大多数RelayCommands有内置的,比如MVVM Light的RelayCommand ,但是其他命令如Microsoft PRISM的DelegateCommand默认没有这种行为。

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

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