我很困惑,試圖綁定一些集合在集合中的屬性而不是元素的屬性。 我甚至不知道如何句話是正確的......代碼可能更好地解釋:這裏是類型(而不是實際的代碼,我已經將它縮短爲基礎):綁定到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