tags:

views:

66

answers:

2

I've got the following collection, which serves as a global storage for a group of LineSeries ItemsSources for a chart.

public ObservableCollection<ObservableCollection<Data>> AllDataSeries;

The said collection may change every now and then, when it's reset I clear the chart from all series - and when it's re-populating again I'm adding as many series to the chart as the AllDataSeries.Count tells me to.

At this point I need to set the binding.

for(int i = 0; i < AllDataSeries.Count; i++)
{
     var series = new LineSeries { IndependentValuePath = "X", DependentValuePath = "Y", Title = "SomeSeriesTitle" };

     Binding binding = new Binding("#?????#");
     binding.Mode = BindingMode.TwoWay;
     binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

     series.SetBinding(ListView.ItemsSourceProperty, binding);
     MyChart.Series.Add(series);
}

The thing is, I have no ide how to compose the Path for the binding. I tried :

String.Format("AllDataSeries[{0}]", i)

but it didn't work.

How should I set the Path property on the binding ?

+1  A: 

Is AllDataSeries actually a field and not a property? I don't think PropertyPath will follow a public field. It's not recommended to expose public fields anyway so you should probably just change that to:

public ObservableCollection<ObservableCollection<Data>> AllDataSeries
{
    get;
    private set;
}

But to answer your question about the binding syntax, I believe your PropertyPath syntax is correct. Though I am nervous about a nested ObservableCollection like that. Personally I would just forego the binding altogether and do something like:

series.ItemsSource = AllDataSeries[i];

It doesn't look like changes to the AllDataSeries collection are going to trigger new series being added anyway.

Did you remember to set the DataContext of the chart? Optionally, you can set the Source property of the Binding.

binding.Source = this;

Finally, you should change BindingMode to OneWay and remove the UpdateSourceTrigger. A chart is a read-only control and so there's no reason to have a two-way binding there.

Hope this helps.

Josh Einstein
Hey, thanks for a detailed response.AllDataSeries is a property (it's past midnight here and I'm not thinking very clearly anymore).I've found an error in the series.SetBinding line, (it should be LinearSeries.ItemsSource property, not ListView.ItemsSource).It still doesn't work, after the SetBinding call the ItemsSource is null. any ideas?I've set the chart's DataContext.
Maciek
Have you tried setting the source of the binding explicitly? I would expect the DataContext to propagate to the series, but I don't really have any experience with the charting controls in the WPF toolkit.
Josh Einstein
I should also reiterate that the second paragraph of your question indicates to me that you're not really gaining anything by setting a binding here. Of course I'd want to know why binding wasn't working too but in the end I think you'd be better off setting ItemsSource = AllDataSeries[i] in this case.
Josh Einstein
I got it to work :) , to satisfy your curiosity - What I'm dealing with is a collection of items that usually contains 40 * 10^6 items (The source can be a cyclic buffer, but is usually sequential). I'm using a virtualization algorithm to display N items from the forementioned collection in a ListView. Whenever I change a selection (for instance having scrolled down 50k items further) I'm updating the chart to see what's going on in the area that I'm currently viewing :)
Maciek
A: 

I feel your pain, I tried to do this myself. "object[0]" works fine with things that are DependencyObjects or INotifyPropertyChanged implementations, but ignores INotifyCollectionChanged. It seems like indexed paths to collections don't quite work, or it just wasn't obvious to me either. I got around it by setting the DataContext of the relevant control to the collection directly and using "." as my path. It worked, but it did make things harder for me later. I really think its that [] syntactical element that is flawed.

Hopefully someone else will have a better idea.

Grant BlahaErath