2012-04-18 56 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

相關問題