如何翻译 CodeBehind WPF 事件;事件、处理程序、EventSetter 到 MVVM 模式?
我正在尝试将 WPF CodeBehid 事件(例如 Event、Handler、EventSetter)转换为 MVVM 模式。我不允许使用 System.Windows.Controls,因为我使用的是 MVVM。我也避免使用第三方库来解决这个问题。
有人可以解释如何将以下 CodeBehind 事件处理程序转换为 MVVM 事件处理程序吗?请在撰写答案时尽可能多地解释。
XAML 代码
<DataGridCheckBoxColumn Header="Select" Binding="{Binding Path=IsSelected}">
<DataGridCheckBoxColumn.CellStyle>
<Style TargetType="DataGridCell">
<EventSetter Event="MouseLeftButtonUp" Handler="ApprovedMouseUp"></EventSetter>
</Style>
</DataGridCheckBoxColumn.CellStyle>
</DataGridCheckBoxColumn>
代码隐藏
private void ApprovedMouseUp(object sender, MouseButtonEventArgs e)
{
if(sender is DataGridCell)
{
var temp = (sender as DataGridCell).Content;
if(temp is CheckBox) (temp as CheckBox).IsChecked = !(temp as CheckBox).IsChecked;
}
}
I am trying to translate WPF CodeBehid events like Event, Handler, EventSetter to MVVM pattern. I am not allowed to use System.Windows.Controls since I am using MVVM. And I am also avoiding 3rd party library to solve this issue.
Can somebody explain how to convert the following CodeBehind Event Handler to MVVM Event-Handler? Please explain as much as you can while writing answer.
XAML Code
<DataGridCheckBoxColumn Header="Select" Binding="{Binding Path=IsSelected}">
<DataGridCheckBoxColumn.CellStyle>
<Style TargetType="DataGridCell">
<EventSetter Event="MouseLeftButtonUp" Handler="ApprovedMouseUp"></EventSetter>
</Style>
</DataGridCheckBoxColumn.CellStyle>
</DataGridCheckBoxColumn>
Code Behind
private void ApprovedMouseUp(object sender, MouseButtonEventArgs e)
{
if(sender is DataGridCell)
{
var temp = (sender as DataGridCell).Content;
if(temp is CheckBox) (temp as CheckBox).IsChecked = !(temp as CheckBox).IsChecked;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关于 MVVM 的经验法则很少......
Models
和ViewModles
不应引用System.Windows.Controls
命名空间。Models
和ViewModles
不应处理事件。为此,请使用 ICommand 接口。RoatedCommand
在模型/视图模型中无效(由于第 2 点)。因此使用 DelegateCommand /RelayCommand
话虽如此,以上几点都是如果您编写了附加行为<,则完全允许 /strong> 在MVVM。
There are few thumb rules regarding MVVM....
Models
andViewModles
should not referSystem.Windows.Controls
namespace.Models
andViewModles
should not handle events. UseICommand
interface for that.RoutedCommand
is not valid in Models / ViewModels (due to point 2). Hence use DelegateCommand /RelayCommand
Having said that, all the above points are perfectly allowed if you have written an Attached Behavior in MVVM.
您有几个选择:
在 XAML 中附加事件处理程序,但事件处理程序所做的唯一事情就是调用视图模型并传递适当的参数(重要的是不要将任何 GUI 级别的项目传递给视图模型 - - 只是执行操作所需的数据)
使用EventToCommand 行为(此处展示)将 ICommand 实例(从您的视图模型)附加到视图中的事件
只要您不尝试在样式或模板中设置这些事件处理程序,我就会建议采用选项#1——只要视图模型实际执行所有工作,就没有铁律禁止您在方便时使用事件处理程序
编辑:选项#1
You have a couple of choices:
Attach the event handler in XAML but the only thing the event handler does is call into the view model passing in the appropriate arguments (it's important not to pass any GUI level items to the view model -- just the data necessary to perform the action)
Use the EventToCommand behavior (showcased here) to attach an instance of an ICommand (from your view model) to an event in your view
As long as you're not trying to set these event handlers up in styles or templates I would recommend pursuing option #1 -- there is no iron law prohibiting you from using event handlers when convenient, as long as the view model is what actually performs all the work
Edit: Option #1
您还可以使用 Caliburn Micro 库将 ViewModel 中的处理程序附加到 View 中的事件。
示例代码:
You can also use Caliburn Micro libraries to be able to attach a handler in ViewModel to an event in View.
Sample code: