6

我有一个简单Label的应该包括.Count一个属性的绑定值ObservableCollection

问题是,结果始终为 0(零)。相同的属性绑定到一个 DataGrid,它可以完美地工作,如果集合中的某些内容发生了变化,它甚至会更新。

我在这里做错了什么?

这是我的代码:

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

该属性看起来像:

public static ObservableCollection<Mitglied> MembersList { get; set; }
4

3 回答 3

2

我只能假设您实际上并未将任何项目添加到集合中。如果你认为你是,你将不得不给我们一个更完整的再现。

这对我来说非常有效:

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

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

顺便说一句,这要简洁得多:

<Label Content="{Binding Items.Count}" ContentStringFormat="Members: {0}"/>
于 2013-08-08T11:21:04.783 回答
0

你可以试试这个...

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>
于 2013-08-08T11:42:00.673 回答
-1

嗨,您必须在 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 使用此 collectionChanged,因此适用于 DataGrid。

于 2013-08-08T11:51:54.177 回答