09-10-2017 04:25 PM
I have added a Grpah with three plots. Added a Legend and wanted to have 3 plots of the graph as the Items to my Legend. Is there any way I can add the Legend items at design time?
<ni:Graph>
<ni:Graph.Plots>
<ni.Plot x:name="x1"/>
<ni.Plot x:name="x2"/>
<ni.Plot x:name="x3"/>
</ni.Graph.Plots>
</ni:Graph>
<ni:Legend items{???} />
Seems Items is read only collection....
Solved! Go to Solution.
09-11-2017 09:48 AM
You can use the ItemsSource
property to populate the Legend
. You can show all items by using the graph itself, or you can limit it to a specific collection, as in this XAML from the Standard\DefaultPlotRenderers
example installed with Measurement Studio:
<ni:Legend ItemsSource="{Binding AllPlots, ElementName=graph}" ItemBackground="{Binding Background, ElementName=graph}" BorderBrush="{Binding BorderBrush, ElementName=graph}" IsTabStop="False" />
09-18-2017 12:55 PM
Hi Paul,
I have 2 graphs on the window. the first graph has only one plot and the second graph has 3 plots.
My Legend needs to contain one item as a source from the first graph and only 2 of the plots as two more items from the second graph. Is there a way to mention plots from 2 different graphs as Itemsource of the Legend?
09-18-2017 01:58 PM
Because of the amount of filtering involved, I think the simplest approach would be to initialize a collection in code-behind to contain the specific plots you were interested in. (If you had wanted to combine all items from multiple collections, then a WPF CompositeCollection
could work as a purely declarative solution.)
The Legend
will try to display the items from any collection you provide to ItemsSource
, so based on your description:
XAML: <ni:Graph x:Name="graph1"> <ni:Graph.Plots> <ni:Plot Label="Graph 1, Plot 1" /> </ni:Graph.Plots> </ni:Graph> <ni:Graph x:Name="graph2"> <ni:Graph.Plots> <ni:Plot Label="Graph 2, Plot 1" /> <ni:Plot Label="Graph 2, Plot 2" /> <ni:Plot Label="Graph 2, Plot 3" /> </ni:Graph.Plots> </ni:Graph> <ni:Legend x:Name="legend" /> Code: var plots = new List<Plot>( ); plots.Add( graph1.Plots[0] ); plots.AddRange( graph2.Plots.Take( 2 ) ); legend.ItemsSource = plots;
12-22-2021 05:40 AM
Hi Paul,
I have an application where I have to plot data based on user selection and show them in a legend. So I have to create plots dynamically and add plot labels from a ViewModel
Then I have binded Legend to these plots and got the labels displayed on legend.
The problem is - clicking on the legend items no longer change the visibility of the plots.
Can you please help?
Thanks,
12-23-2021 11:05 AM - edited 12-23-2021 11:41 AM
[I see you posted a new question, so I have moved my answer there]
12-20-2022 09:47 AM
This sounds like how you would customize the legend in Windows Forms, not WPF?