【发布时间】:2014-06-21 06:21:02
【问题描述】:
所以我尝试使用以下 XAML 将 TextBlock 绑定到 ObserverableCollection 的 Count 属性
<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>
而且它确实有效。当在 Manager 的静态构造函数中初始化的集合中有 233 个对象时,它显示错误的计数为 52。当对象从集合中移除时,它永远不会更新
编辑 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 个对象。
-
您需要按照 fmunkert 在回答中的建议在 Source 中使用
x:Static。
标签: .net wpf xaml binding static