2012-04-18 77 views
1

我很困惑,试图绑定一些集合在集合中的属性而不是元素的属性。 我甚至不知道如何句话是正确的......代码可能更好地解释:这里是类型(而不是实际的代码,我已经将它缩短为基础):绑定到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是项目的属性,但我如何访问该集合的属性,如PtotalPmin等?

感谢您花时间阅读本文。如果缺少任何信息,只需指出并发布即可。

+0

重新标记以澄清它是Silveright 4 – Alex 2012-04-18 14:36:00

回答

0

访问它原来的客户改变了他的主意电网头,他不希望总在显示标题了。

顺便说一下,我一定尝试了20种不同的方法,包括在各种类型的转换器中进行修补,但是我还没有能够完成这种不那么简单,看起来很明显的任务。

再次感谢有趣的建议。

0

所以你需要集合对象作为绑定源。如果DataGrid是您的自定义myTemplatedControl

<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, 
AncestorType={x:Type myTemplatedControl}}, Path=DataContext.Ptotal}" /> 

您需要这些:

RelativeSource MarkupExtension

Binding.RelativeSource Property

像这样(未测试)。它并不完全清楚什么是myGridObject。其主要思想是:

正如MSDN文档说:Binding.RelativeSource Gets or sets the binding source by specifying its location relative to the position of the binding target.

如果你坚持的X:类型的扩展名,这里是一个关于它的链接,这样你就可以与您的自定义控制使用它:

X:Type

另一种方法是,如果你的名字你的容器元素(如您的收藏是DataContext的),那么你可以设置元素为绑定源:

<TextBlock Text="{Binding ElementName=yourElementName, Path=DataContext.Ptotal}" /> 
+0

谢谢。我正在为“另一种方法”开枪,看起来恰到好处。 – Alex 2012-04-18 14:42:15

+0

代码不希望今天符合:elementName方法已解决例外,但textblock保持空白。会调整更多。为了澄清,'myGridObject'是“主”应用程序中模板化控件的名称 – Alex 2012-04-18 15:00:30

+0

在调试应用程序时,在VS的输出窗口中检查绑定异常。有没有任何绑定异常? – 2012-04-18 15:03:03

0

我认为问题是DataGrid绑定到集合,并且每一行绑定到单个项目,而不是集合。你需要获得一个级别的链接(回到集合本身)。

如果您运行Silverlight 4+,则可以使用relativesource。例如:

<TextBlock Text="{Binding Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType=sdk:DataGrid, AncestorLevel=1}, Path=DataContext.Count}" 

否则可能创建上下文的静态访问通过结合Source

+0

谢谢。立即尝试此操作。 – Alex 2012-04-18 14:35:16

+0

'AncestorType'和'AncestorLevel'似乎不存在于'RelativeSource'类型上,不幸的是,原来的代码不会被编译。 – Alex 2012-04-18 14:41:15

+0

我认为只适用于Silverlight 5,你可以升级你的项目吗? – user1341566 2012-04-18 19:47:46

相关问题