I’m confused while trying to bind some properties housed inside a collection rather than the properties of the elements.
I’m not even sure how to phrase it right… code might explain better: here are the types (not actual code, I’ve shortened it to the basics):
public class myType
{
public int P {get;set;}
}
public class myTypeCollection : ObservableCollection<myType>
{
public int Ptotal {get { return this.Items.Select(i=>i.P).Aggregate((c,t)=>t = t + c); }}
public int Pmin { get { this.Items.Min(i => i.P); } } //concept
public int Pmax { get { this.Items.Max(i => i.P); } } //concept
}
They’re being used in a templated control, whose XAML looks like this:
(adding comments to make it as clear as i’m able to)
<!-- myGridObject = new myTemplatedControl(); -->
<!-- myGridObject.DataContext = new myTypeCollection(); -->
<!-- NOTE: collection obviously is NOT empty in the real code -->
<sdk:DataGrid ItemsSource={Binding DataContext}>
<sdk:DataGridTemplateColumn Width="Auto">
<sdk:DataGridTemplateColumn.HeaderStyle>
<Style TargetType="sdk:DataGridColumnHeader">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<!-- ?????? write out Ptotal in the header of the column ??????? -->
<!-- This throws a binding-related ArgumentException -->
<TextBlock Text="{Binding ???? Ptotal ?????}" />
<!-- closing tags cut off -->
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding P}" />
<!-- closing tags cut off once more-->
{Binding P} works as expected, since P is a property of the items, but how do I access the collection’s properties like Ptotal, Pmin, etc. ?
Thanks for taking the time to read this. If any info is missing just point it out and I’ll post it.
Turns out the customer changed his mind about the grid headers, he doesn’t want total displayed in the header anymore.
By the way, I must have tried 20 different approached including patching in various types of converters but I haven’t been able to accomplish this not-as-simple-as-it-looks-apparently task.
Thanks again for the nonetheless interesting suggestions.