Reputation: 774
So I am trying to bind a TextBlock to the Count property of a ObserverableCollection with the following XAML
<TextBlock x:Name="inactiveCount">
<TextBlock.Text>
<Binding Path="(sockets:Manager.inactiveCollection.Count)" Mode="OneWay" StringFormat="Inactive: {0}" />
</TextBlock.Text>
</TextBlock>
But I receive a exception when running the program:
Type reference cannot find type named '{clr-namespace:MyProgram.Net.Sockets;assembly=MyProgram}Manager.inactiveCollection'.
Binding to other properties in the Manager class work fine, and inactiveCollection certainly does exist as a static property. What exactly am I doing wrong?
Edit
So per fmunkert's suggestions below i've changed it to
<TextBlock x:Name="inactiveCount">
<TextBlock.Text>
<Binding Path="Count" Source="(sockets:Manager.inactiveCollection)" Mode="OneWay" StringFormat="Inactive: {0}" />
</TextBlock.Text>
</TextBlock>
and it kind of works. It displays the wrong number for the count, 52 when there are 233 objects in the collection which is initialized in Manager's static constructor. And it never updates as objects are removed from the collection
Edit 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
}
}
}
Upvotes: 0
Views: 185
Reputation: 774
So fmunkert was partially right, I needed to specify a source, but I also needed to specify a StaticResource for the class in Window.Resources. The following XAML worked for me:
<Window.Resources>
<sockets:Manager x:Key="Manager"/>
</Window.Resources>
<TextBlock x:Name="inactiveCount" Text="{Binding Path='inactiveCollection.Count', Source='{StaticResource Manager}', StringFormat='Inactive: {0}'}"/>
Upvotes: 0
Reputation:
Use {x:Static}
for the binding source, e.g. like this:
<TextBlock Text="{Binding Path=Count, Source={x:Static sockets:Manager.inactiveCollection}, Mode=OneWay, StringFormat='Inactive: {0}'}"/>
Upvotes: 1