使用 WPF 将 ObservableCollection.Count 绑定到标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18124125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:23:42  来源:igfitidea点击:

Binding an ObservableCollection.Count to Label with WPF

wpfcountbindobservablecollectionzero

提问by Pascal

I have a simple Labelthat should include the bound .Countvalue of a Property of an ObservableCollection.

我有一个简单的Label,应该包括.Count一个属性的绑定值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.

问题是,结果始终为 0(零)。相同的属性绑定到 DataGrid,它可以完美运行,甚至在集合中发生变化时也会更新。

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; }

采纳答案by Vishal

You can try This...

你可以试试这个...

MainWindow.Xaml.cs->

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->

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>

回答by Kent Boogaart

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

主窗口.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

主窗口.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}"/>

回答by yo chauhan

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

嗨,您必须通知 CollectionChanged,然后它才能工作

    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.

DataGrid 使用此 collectionChanged,因此适用于 DataGrid。