我在尝试绑定集合中的一些属性而不是元素的属性时感到困惑。我什至不确定如何正确表达它...代码可能会更好地解释:这里是类型(不是实际代码,我已将其缩短为基础):
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
}
它们正在模板化控件中使用,其 XAML 如下所示:(添加注释以使其尽可能清晰)
<!-- 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}
可以按预期工作,因为P
它是项目的属性,但是如何访问集合的属性,例如Ptotal
,Pmin
等?
感谢您抽时间阅读。如果缺少任何信息,请指出,我会发布它。