4

I'm trying to get a "Copy" context menu to display when clicking on the advanced options icon of any property. What do I need to add to make it work?

<xctk:PropertyGrid
        x:Name="PropertyGrid"  
        Grid.Column="1" Margin="8"
        ShowSummary="False"    
        AutoGenerateProperties="True" 
        HideInheritedProperties="False"
        SelectedObject="{Binding InspectedObject}"
        SelectedObjectName="{Binding InspectedObject, Converter={StaticResource PropertyGridPropertyNameConverter}}"
        SelectedObjectTypeName="{Binding InspectedObject, Converter={StaticResource PropertyGridPropertyTypeConverter}}"
        SelectedObjectChanged="PropertyGrid_OnSelectedObjectChanged"
        ShowAdvancedOptions="True"

    >
    <xctk:PropertyGrid.AdvancedOptionsMenu >
        <ContextMenu>
            <MenuItem Header="Copy" Click="MenuItem_OnClick"></MenuItem>
        </ContextMenu>
    </xctk:PropertyGrid.AdvancedOptionsMenu>
</xctk:PropertyGrid>

I'd like the "Copy" context item to display and take a click whether or not the property is read only.

jchristof
  • 2,794
  • 7
  • 32
  • 47
  • Did you find the answer to this? Can you share? Thanks :) – IgorMF Jul 16 '17 at 16:05
  • No, sorry I haven't addressed it yet - I've been putting off having to work around this by either making the properties writable or making custom controls which would allow copying (I have lots of properties) – jchristof Jul 17 '17 at 13:52
  • 1
    @IgorMF I forked the original repo and I have added copy value menu item to AdvancedOptionsMenu. Check it out: https://github.com/kmatyaszek/wpftoolkit – kmatyaszek Aug 24 '18 at 15:17

1 Answers1

1

In the MenuItem click event handler, you can access property data by property DataContext in sender object.

private void MenuItem_OnClick(object sender, RoutedEventArgs e)
{
    MenuItem menuItem = sender as MenuItem;
    if (menuItem != null && menuItem.DataContext is PropertyItem)
    {
        Clipboard.SetData(DataFormats.Text, ((PropertyItem)menuItem.DataContext).Value);
    }
}

In the following link, you can find more information about this topic: https://kmatyaszek.github.io/2018/08/22/extended-wpftoolkit-propertygrid-copybutton.html

I have noticed that this solution works when you clicked (left mouse button) at AdvancedOptionsMenu icon, there is a problem with the right mouse click on the property item. To solve this problem I forked the original repo and I have added copy value menu item to AdvancedOptionsMenu. Check it out: https://github.com/kmatyaszek/wpftoolkit

kmatyaszek
  • 19,016
  • 9
  • 60
  • 65
  • @Gaspa79 I have changed the blog engine. Please read this post on the following site: https://kmatyaszek.github.io/2018/08/22/extended-wpftoolkit-propertygrid-copybutton.html – kmatyaszek Sep 03 '20 at 20:45