所以我想給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
}
}
}
也許你應該發佈代碼與'Manager.inactiveCollection'的聲明和代碼,顯示你如何添加/刪除項目。 –
完成。它的實際人羣與一些SQL查詢相關,它是如何填充的並不是非常重要,在靜態構造函數的末尾它包含了233個對象。 – Matt
您需要按照fmunkert的建議,在Source中使用'x:Static'。 –