【问题标题】:WpfToolkit PropertyGridWpfToolkit PropertyGrid
【发布时间】:2017-07-17 03:34:12
【问题描述】:

我正在尝试以 MVVM 友好的方式从 PropertyGridExtended WPF Toolkit™ by Xceed 绑定到 PreparePropertyItem 事件:

<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>

只有当我点击展开 [ExpandableObject] 时才会调用我的自定义 PreparePropertyCommand

这真的很奇怪,因为如果我只是绑定到事件,它就可以直接工作:

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

当然,这会破坏 MVVM 模型,因为 PropertyGrid_PreparePropertyItem 在视图的代码隐藏中进行。

有什么见解吗?谢谢!

【问题讨论】:

    标签: wpf mvvm wpftoolkit propertygrid icommand


    【解决方案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 中做所有事情 - 它是关于关注点分离。

    【讨论】:

      猜你喜欢
      • 2013-10-30
      • 2019-01-31
      • 2010-10-17
      • 2010-10-13
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 2011-03-08
      • 1970-01-01
      相关资源
      最近更新 更多