05-10-2017 09:50 AM
Is there any way with the WPF Legend control to detect the type of item the user clicks on when they right click to open a context menu? I have an application showing plots and cursors in the legend. When the user clicks on a cursor, I want to serve up a context menu for that cursor. If they click on a plot, I want to present a context menu for that plot. Not only do I need to know the type of item, but also which item specifically they click on.
I do exactly this in WinForms with the legend control there. There does not appear to be a similar mechanism in WPF. Am I missing something?
05-11-2017 12:49 PM
Hi dpstark,
What version of Measurement Studio are you using? When doing this with WinForms are you using a reference to the item type?
Thanks,
ShaneK
Applications Engineering
05-11-2017 12:57 PM
Measurement Studio 2015 for VS 2013.
In WinForms, I use
legend.GetItemAt(e.X, e.Y)
where e is the MouseEventArgs from a MouseDown event to retrieve the LegendItem at that point (or null). If it's a cursor, I display a cursor menu. If it's a plot, the plot menu, etc.
I can't find a similar method for WPF Legend because it only returns that a label, button, etc., was clicked- not a LegendItem.
05-11-2017 05:36 PM
The legend does not expose any programmatic way to access the items it displays. However, like an ItemsControl, the legend supports customization through data templates. For example, to show a right-click ContextMenu, you could declare a data template resource like that shown below:
<ni:Legend.Resources>
<DataTemplate DataType="{x:Type ni:Plot}">
<StackPanel Orientation="Horizontal" Background="Transparent">
<StackPanel.ContextMenu>
<ContextMenu>
<MenuItem Header="Plot Menu" />
</ContextMenu>
</StackPanel.ContextMenu>
<niPrimitives:LegendGlyph Renderer="{Binding ActualRenderer}"
Background="{Binding ItemBackground, RelativeSource={RelativeSource AncestorType={x:Type ni:Legend}, Mode=FindAncestor}}"
Width="{Binding GlyphSize.Width, RelativeSource={RelativeSource AncestorType={x:Type ni:Legend}, Mode=FindAncestor}}"
Height="{Binding GlyphSize.Height, RelativeSource={RelativeSource AncestorType={x:Type ni:Legend}, Mode=FindAncestor}}" />
<ContentPresenter Margin="3"
VerticalAlignment="Center"
Content="{Binding Label}"
ContentTemplate="{Binding LabelTemplate}" />
</StackPanel>
</DataTemplate>
</ni:Legend.Resources>