【发布时间】:2012-04-18 13:52:03
【问题描述】:
我在尝试绑定集合中的一些属性而不是元素的属性时感到困惑。 我什至不确定如何正确表达它...代码可能会更好地解释:这里是类型(不是实际代码,我已将其缩短为基础):
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 如下所示: (添加 cmets 使其尽可能清晰)
<!-- 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
标签: c# binding silverlight-4.0 collections datagrid