2014-06-21 210 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 ”吗? –