我试图以MVVM友好的方式从PropertyGrid
of Extended WPF Toolkit™ by Xceed
绑定到Extended WPF Toolkit™ by Xceed
事件:
<UserControl x:Class=(...)
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:mvvm="http://prismlibrary.com/"
(...)
<xctk:PropertyGrid x:Name="PropertyGrid" SelectedObject="{Binding}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreparePropertyItem">
<mvvm:InvokeCommandAction Command="{Binding PreparePropertyCommand}"/> //PRISM's InvokeCommandAction doesn't work
<i:InvokeCommandAction Command="{Binding PreparePropertyCommand}"/> //BLEND's InvokeCommandAction doesn't work either
</i:EventTrigger>
</i:Interaction.Triggers>
</xctk:PropertyGrid>
在加载或显示属性网格时,只在单击以展开PreparePropertyCommand时,才调用自定义[ExpandableObject]
。
这真的很奇怪,因为如果我简单地绑定到事件,它就会直接工作:
<xctk:PropertyGrid x:Name="PropertyGrid" SelectedObject="{Binding}" PreparePropertyItem="PropertyGrid_PreparePropertyItem">
当然,这破坏了MVVM模型,因为PropertyGrid_PreparePropertyItem
继续对视图进行代码隐藏。
有什么见解吗?谢谢!
发布于 2017-07-17 12:32:16
事件触发器不能工作的原因是PreparePropertyItem是一个附加事件:http://joyfulwpf.blogspot.se/2009/05/mvvm-invoking-command-on-attached-event.html
当然,这破坏了MVVM模型,因为PropertyGrid_PreparePropertyItem继续对视图进行代码隐藏。
如果您只是从XAML标记定义的完全相同视图的代码背后调用命令,则不会:
private void PropertyGrid_PreparePropertyItem(object sender, Xceed.Wpf.Toolkit.PropertyGrid.PropertyItemEventArgs e)
{
YourViewModel vm = PropertyGrid.DataContext as YourViewModel;
if (vm != null)
vm.PreparePropertyCommand.Execute(null);
}
MVVM不是要从视图中删除与视图相关的代码,而是要在XAML中完成所有事情--它是关于关注点的分离。
https://stackoverflow.com/questions/45135597
复制相似问题