我很困惑,试图绑定一些集合在集合中的属性而不是元素的属性。 我甚至不知道如何句话是正确的......代码可能更好地解释:这里是类型(而不是实际的代码,我已经将它缩短为基础):绑定到Datagrid的ItemsSource集合属性而不是单个项目
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
等?
感谢您花时间阅读本文。如果缺少任何信息,只需指出并发布即可。
重新标记以澄清它是Silveright 4 – Alex 2012-04-18 14:36:00