Search code examples
wpfmvvmwpftoolkitpropertygridicommand

WpfToolkit PropertyGrid


I'm trying to bind to the PreparePropertyItem event from PropertyGrid of Extended WPF Toolkit™ by Xceed in MVVM-friendly way:

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

My custom PreparePropertyCommand is not being called when the propertygrid gets loaded or shown, only when I click to expand an [ExpandableObject]

This is really strange since it works straight out if I simply bind to event:

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

of course this breaks MVVM model since PropertyGrid_PreparePropertyItem goes on code-behind of the view.

Any insights? Thanks!


Solution

  • The reason why your event trigger won't work is that PreparePropertyItem is an attached event: http://joyfulwpf.blogspot.se/2009/05/mvvm-invoking-command-on-attached-event.html

    of course this breaks MVVM model since PropertyGrid_PreparePropertyItem goes on code-behind of the view.

    Not if you simply invoke the command from the code-behind of very same view that your XAML markup is defined in:

    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 is not about eliminating view-related code from the views and do everything in XAML - It is about separation of concerns.