2017-07-17 91 views
0

我试图从Extended WPF Toolkit™ by XceedPropertyGrid在MVVM友好的方式绑定到PreparePropertyItem事件:WpfToolkit PropertyGrid的

<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当PropertyGrid中被加载或显示没有被调用,只有当我点击展开的[ExpandableObject]

这是很奇怪的,因为它的工作原理直出,如果我只是绑定到事件:

<xctk:PropertyGrid x:Name="PropertyGrid" SelectedObject="{Binding}" PreparePropertyItem="PropertyGrid_PreparePropertyItem"> 

当然这打破了MVVM模型,因为PropertyGrid_PreparePropertyItem在视图的代码隐藏之上。

任何见解?谢谢!

回答

1

之所以你的事件触发将无法正常工作是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中查看和完成所有事情 - 关注点分离。

+0

很好的答案,谢谢! – IgorMF