06-06-2013 05:24 PM
Hi,
Is there a way to bind plots (plot collection) to a graph.
I'm using a graph with multiple plots. I bind the datasource with an observablecollection for the data values => the default plots are shown, perfect.
but now I want to manage these plots (color, axis etc..). For that I have to use plots. I can define manually plots in XAML.
but I want to add or remove plots at runtime. The datasource is perfect for data values but I have not found the same for plots. In the MVVM concept, is there a way to bind plots with a collection.
Thanks for any help.
O .Daniere
06-07-2013 09:56 AM
There is no equivalent to DataSource
for plots. There is a DefaultPlotRenderers
collection for customizing the default appearance of plots, but nothing for configuring other properties like associated axes.
There is a primitve type available that can be used to synchronize a model collection with e.g. the Plots
collection on a graph:
var plots = new ObservableCollection<Plot>( ... );
var synchronizer = new NationalInstruments.Controls.Primitives.CollectionViewSynchronizer( );
synchronizer.SourceCollection = CollectionViewSource.GetDefaultView( plots );
synchronizer.TargetCollection = graph.Plots;
You could use this in code, or create an attached "GraphExtensions.PlotsSource" dependency property that performs the same action when the property value changes.
06-16-2013 08:15 AM
Thanks for your response Paul.
I have another question regarding the PlotRenderer binding :
Using XAML I can bind the plot color to a class property like this :
<ni:Graph x:Name="MyGraph">
<ni:Graph.Plots>
<ni:Plot Label="Plot 1">
<ni:LinePlotRenderer Stroke="{Binding PlotLineColor}" />
</ni:Plot>
</ni:Graph.Plots>
</ni:Graph>
With this code behind in VB.NET 2012 (MyGraphVM contains the PlotLineColor property):
MyGraph.Plots(0).DataContext = MyGraphVM
And it works.
But, I can't do the same binding by code (for example using Plot.setBinding()).
Do you have an example?
What is the best way to bind plot's properties (color, shape, etc)?
O. Daniere.
06-17-2013 10:12 AM
You can use the BindingOperations.SetBinding
method to set a binding on a plot renderer:
var renderer = plot.Renderer;
var binding = new Binding( "PlotLineColor" ) { Source = MyGraphVM };
BindingOperations.SetBinding( renderer, LinePlotRenderer.StrokeProperty, binding );
06-20-2013 03:12 PM
Thanks Paul, it works perfectly.
O. Daniere
07-08-2013 01:29 PM
By the way, I have created the "GraphExtensions.PlotsSource" example I mentioned earlier in an answer to another question.