2014-06-21 109 views
0

所以我想給TextBlock到ObserverableCollection的Count屬性綁定以下XAML.NET 4.5/WPF - 綁定到一個靜態屬性的屬性

<TextBlock x:Name="inactiveCount"> 
    <TextBlock.Text> 
     <Binding Path="(sockets:Manager.inactiveCollection.Count)" Mode="OneWay" StringFormat="Inactive: {0}" /> 
    </TextBlock.Text> 
</TextBlock> 

但我運行時收到一個異常程序:

Type reference cannot find type named '{clr-namespace:MyProgram.Net.Sockets;assembly=MyProgram}Manager.inactiveCollection'. 

綁定到Manager類中的其他屬性工作正常,並且inactiveCollection肯定確實存在作爲靜態屬性。我究竟做錯了什麼?

編輯

所以每fmunkert的建議,下面,我把它改成

<TextBlock x:Name="inactiveCount"> 
    <TextBlock.Text> 
     <Binding Path="Count" Source="(sockets:Manager.inactiveCollection)" Mode="OneWay" StringFormat="Inactive: {0}" /> 
    </TextBlock.Text> 
</TextBlock> 

和它種工作。它顯示計數錯誤的編號,52當集合中有233個對象在管理器的靜態構造函數中初始化時。作爲對象從集合

編輯2

namespace MyProgram.Net.Sockets 
{ 
//technically this is ProxyClientManager but I renamed it here to make the code smaller 
    class Manager 
    { 
     public static ObservableCollection<ProxyClient> inactiveCollection { get; set; } 

     static Manager() 
     { 
      inactiveCollection = new ObservableCollection<ProxyClient>(); 
      //do stuff to populate inactiveCollection with 233 objects 
      //do other stuff like start threads to add/remove objects from the collection 
     } 
    } 
} 
+0

也許你應該發佈代碼與'Manager.inactiveCollection'的聲明和代碼,顯示你如何添加/刪除項目。 –

+0

完成。它的實際人羣與一些SQL查詢相關,它是如何填充的並不是非常重要,在靜態構造函數的末尾它包含了233個對象。 – Matt

+0

您需要按照fmunkert的建議,在Source中使用'x:Static'。 –

回答

0

所以fmunkert是部分正確的,我需要指定源,但我還需要到指定的StaticResource在Window.Resources類。下面的XAML爲我工作:

<Window.Resources> 
    <sockets:Manager x:Key="Manager"/> 
</Window.Resources> 
<TextBlock x:Name="inactiveCount" Text="{Binding Path='inactiveCollection.Count', Source='{StaticResource Manager}', StringFormat='Inactive: {0}'}"/> 
1

使用{x:Static}爲綁定源,例如移除,它永遠不會更新像這樣:

<TextBlock Text="{Binding Path=Count, Source={x:Static sockets:Manager.inactiveCollection}, Mode=OneWay, StringFormat='Inactive: {0}'}"/> 
+0

只是想知道,但你會碰巧知道爲什麼它顯示一個不正確的綁定值?例如,當集合中有233個對象時,它會顯示「無效:52」?另外它似乎並沒有更新所有對象被添加/從集合中刪除? – Matt

+0

另外它告訴我什麼時候使用Text值,在ProxyClientManager中找不到inactiveCollection。但是它會發現它使用了XAML以及我在第一條評論中提到的更改。 – Matt

+0

ProxyClientManager類與'Manager.inactiveCollection'屬性有什麼關係? 「Manager.inactiveCollection」的數據類型真的是「ObservableCollection 」嗎? –