Is is possible to create a collection of LineGraphs in Xaml?

Jun 3, 2011 at 3:05 PM

Hi,

I'd like to create a collection of LineGraphs by binding to the ItemsSource of an appropriate collection and using a ItemsDataTemplate to create the individual LineGraphs with a xaml file.

Is this possible?

thanks,

Danny

Jun 3, 2011 at 4:16 PM
Edited Jun 3, 2011 at 4:17 PM

I did something pretty similar today.  This code lets your bind a new "LineGraphs" property in a chart plotter to a IPointDataSource[] array in your view model.

add this to ChartPlotter.cs :

public static readonly DependencyProperty LineGraphsProperty =
            DependencyProperty.Register("LineGraphs", typeof(IPointDataSource[]), typeof(ChartPlotter), new FrameworkPropertyMetadata(new IPointDataSource[0],
                FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(LineGraphsCallback)));
        public IPointDataSource[] LineGraphs
        {
            get { return (IPointDataSource[])GetValue(LineGraphsProperty); }
            set
            {
                SetValue(LineGraphsProperty, value);
               
            }
        }
        private static void LineGraphsCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null && e.NewValue is IPointDataSource[])
            {
                (d as ChartPlotter).Children.ClearLineGraphs();
                int x = 0;
                foreach (IPointDataSource source in e.NewValue as IPointDataSource[])
                {
                    if (source != null)
                    {
                        (d as ChartPlotter).AddLineGraph(source, 2, x+"");
                        x++;
                    }
                }
            }
        }

and in the constructor add : SetValue(LineGraphsProperty, new IPointDataSource[0]);

and in ChildCollection.cs :

public void ClearLineGraphs()
        {
            var items = new List<IPlotterElement>(base.Items);
            foreach (var item in items)
            {
                if (item is LineGraph) Remove(item);
            }
        }

now in your view: <d3:ChartPlotter LineGraphs="{Binding Lines}"/>

where lines is a IPointDataSource[]

Jun 5, 2011 at 4:38 PM

Hi arghdos,

thanks so much for the rapid response - the solution looks good. Would the next step be to implement a LineGraphsItemTemplate property and to check for a DataTemplate when instantiating each item in the ItemsSource?

I'll post any successful solution but look forward to guidance in this.

regards,

Danny

Jun 6, 2011 at 8:47 PM

In my program my ViewModel has this:

public IPointDataSource[] Lines
        {
            get
            {
                if (Tags.Count <= 0) return null;
                mySource = null;
                mySource = (from tag in Tags
                            select tag.data as IPointDataSource).ToArray();
                return mySource;
            }
        }

where Tags is a collection of Tag objects (Tag.data is a IPointDataSource)

and the view itself has

<d3:ChartPlotter LineGraphs="{Binding Lines}"/>