7

I have a simple Label that should include the bound .Count value of a Property of an ObservableCollection.

The thing is, that the result is always 0 (zero). The same Property is bound to a DataGrid, which works perfectly and even updates if something has changed in the Collection.

What am I doing wrong here?

Here is my code:

<Label ContentStringFormat="Members: {0}">
    <Label.Content>
        <Binding Path="MembersList.Count" Mode="OneWay" UpdateSourceTrigger="Default" />
    </Label.Content>
</Label>

The Property looks like:

public static ObservableCollection<Mitglied> MembersList { get; set; }
g t
  • 7,287
  • 7
  • 50
  • 85
Pascal
  • 73
  • 1
  • 1
  • 4
  • 1
    Are you implementing INotifyPropertyChanged on your data bound class? – Phil Murray Aug 08 '13 at 10:59
  • which class do you mean? Mitglied? Yes I do – Pascal Aug 08 '13 at 11:01
  • try this – JSJ Aug 08 '13 at 11:03
  • Whichever class the MembersList property is in. In order for the binding to work is you are loading the collection from an external source you need to implement INotifyPropertyChanged on the class and raise the PropertChanged event when the Set is called on the property. http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx – Phil Murray Aug 08 '13 at 11:04
  • I did, doesn't work :/ – Pascal Aug 08 '13 at 11:04
  • My spidey-sense is telling me to try to remove the `ContentStringFormat`. – Kent Boogaart Aug 08 '13 at 11:05
  • 3
    BTW everyone, `ObservableCollection` *does* implement INPC, and does raise `PropertyChanged` for the `Count` property. – Kent Boogaart Aug 08 '13 at 11:06
  • Ok. I try'd to implement INotifyPropertyChanged. This doesn't solves the problem. – Pascal Aug 08 '13 at 11:09
  • Also I removed the `ContentStringFormat`. This. The Values is 0. – Pascal Aug 08 '13 at 11:09
  • Any idea? I think I've read something about binding integers will not work? what makes me wonder is that the datagrid has no problems with the ObservableCollection – Pascal Aug 08 '13 at 11:18
  • It may be related to your property being static. http://stackoverflow.com/questions/936304/binding-to-static-property – Phil Murray Aug 08 '13 at 11:20

3 Answers3

3

I can only assume you've not actually added any items to the collection. If you think you are, you'll have to give us a more complete repro.

This works perfectly for me:

MainWindow.xaml

<Window x:Class="SO18124125.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <Button x:Name="addButton">Add</Button>
        <Label>
            <Label.Content>
                <Binding Path="Items.Count" Mode="OneWay" UpdateSourceTrigger="Default"/>
            </Label.Content>
        </Label>
    </StackPanel>
</Window>

MainWindow.xaml.cs

namespace SO18124125
{
    using System.Collections.ObjectModel;
    using System.Windows;

    public partial class MainWindow : Window
    {
        private static readonly ObservableCollection<string> items = new ObservableCollection<string>();

        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = this;

            this.addButton.Click += delegate
            {
                items.Add("foo");
            };
        }

        public static ObservableCollection<string> Items
        {
            get { return items; }
        }
    }
}

BTW, this is far more succinct:

<Label Content="{Binding Items.Count}" ContentStringFormat="Members: {0}"/>
Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
  • But it's an ObservableCollection. Shouldn't it update? After the UI initialized the Collection gets filled. Datagrid refreshes automaticly. The label values stays to 0. – Pascal Aug 08 '13 at 11:25
  • Ok. I did the same. No changes. – Pascal Aug 08 '13 at 11:32
  • That's actually what I'm doing at the start. public MainWindow() { Var.MembersList = new ObservableCollection(); Var.SchoolList = new ObservableCollection(); InitializeComponent(); ConnectToDataBase(); Var.MembersList = new ObservableCollection(SQL.selectMitglieder()); } But it doesn't work for me... – Pascal Aug 08 '13 at 11:41
0

You can try This...

MainWindow.Xaml.cs->

int Counter = 0;
private static ObservableCollection<string> _MemberList = new ObservableCollection<string>();
// Suppose it is of String type..I took it as of String type to check my case
public static ObservableCollection<string> MemberList
{
    get { return MainWindow._MemberList; }
    set { MainWindow._MemberList = value; }
}

MainWindow()
{
    InitializeComponent();
    MemberList.Add("0");
    MemberList.Add("1");
    MemberList.Add("2");
    Label1.DataContext = this;
}

private void Button_Click(object sender, RoutedEventArgs e)
{          
    try
    {
        MemberList.RemoveAt(Counter);
        Counter++;
     }
     catch(Exception ex)
     {
         string strTemp=ex.Message();
     }
 }

MainWindow.xaml->

    <Grid>
        <Label Name="Label1" ContentStringFormat="Members: {0}" Margin="0,56,141,38" RenderTransformOrigin="0.158,1.154" HorizontalAlignment="Right" Width="183">
        <Label.Content>
            <Binding Path="MemberList.Count" Mode="OneWay" UpdateSourceTrigger="Default"/>
        </Label.Content>
    </Label>
        <Button Click="Button_Click" Width="100" Height="20" Content="click" Margin="43,169,360,122" />
    </Grid>
Łukasz
  • 8,555
  • 2
  • 28
  • 51
Vishal
  • 604
  • 1
  • 12
  • 25
  • 1
    Ok. So you where actually wright. I had to add the Items before Initializing the UI. No it works and Updates perfectly. Thank You! – Pascal Aug 08 '13 at 11:48
-1

Hi You will have to Notify on CollectionChanged then it will Work

    public class ViewModel: INotifyPropertyChanged
{
    public ObservableCollection<string> MembersList { get; set; }

    public ViewModel()
    {
        MembersList = new ObservableCollection<string>();
        MembersList.CollectionChanged += collection_CollectionChanged;
        MembersList.Add("wfwef");
    }

    void collection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        Notify("MembersList.Count");
    }

    private void Notify(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }


    public event PropertyChangedEventHandler PropertyChanged;
}

DataGrid uses this collectionChanged and hence working for DataGrid.

yo chauhan
  • 12,079
  • 4
  • 39
  • 58