2017-07-17 92 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