1

Hi everyone i am working on a project which is configuring a networking device via telnet,i am using C# Wpf. The problem is i have a Observable Collection and i want to display number of items present in the observable collection but it is not displaying . I have tried following :

 public ObservableCollection<VLANSPropertyClass> vlan { get; set; }

  public int Vlans
        {
            // Retreive value from Configuration Library
            get
            {
                return this.vlan.Count;

            }
        }

XAML is :

 <TextBlock Margin="3,0"  
        Style="{StaticResource SummaryValues}"  
        Text="{Binding Path=Vlans}"
        Visibility="Visible"
        />

Now it is not displaying anything .Any help would be highly appreciable :)

CodeTheft
  • 91
  • 1
  • 9

3 Answers3

3

Easiest is to simply bind to vlan.Count (in my example i used textbox, but textBlock won't need OneWay). No extra property or INotifyPropertyChanged needed. Adding to the collection will update the count automatically.

<TextBox Text="{Binding vlan.Count, Mode=OneWay}" />

or

<TextBlock Text="{Binding vlan.Count}" />
1

The binding does not know when to update the target. You need to implement INotifyPropertyChanged, subscribe to .CollectionChanged and raise a PropertyChanged event when Vlans (the count) changes.

James Willock
  • 1,999
  • 14
  • 17
0

You can use a ListCollectionView on top of the ObservableCollection.

In your ViewModel, define them as:

    ObservableCollection<VLANSPropertyClass> vlan;
    public System.Windows.Data.ListCollectionView CountingView { get; private set; }

Then, initialize the collection to the ObservableCollection:

CountingView = new System.Windows.Data.ListCollectionView(vlan);

In your xaml, bind to the Count property of the collection:

<TextBlock Margin="5" Height="35" Width="50" Text="{Binding CountingView.Count}" />

I hope this helps!

ShayD
  • 920
  • 8
  • 18