Binding an ObservableCollection.Count to Label with WPF

只谈情不闲聊 提交于 2019-11-30 23:25:28
Vishal

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>

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}"/>

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!