如何在WPF中使用ObservableCollection绑定ItemsSource

时间:2013-10-06 17:28:56

标签: c# .net wpf xaml

在我的WPF应用程序中 - 我通过ObservableCollection将新项目添加到Button Click Event Handler。现在,我希望立即显示此添加的项目,因为它通过ObservableCollection添加到BindingItemsControl我编写了代码,但它无效。谁能解决我的问题。这里的代码是:

.XAML文件

    <dxlc:ScrollBox VerticalAlignment="Top">
        <ItemsControl x:Name="lstItemsClassM" ItemsSource="{Binding Path=topp,   Mode=TwoWay}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <Button Content="{Binding Name}"  Tag="{Binding PKId}"/>
                      </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

    </dxlc:ScrollBox>

.CS文件

     public ObservableCollection<ClassMM> topp { get; set; }

    int dv , startindex, lastindex;

    public MainWindow()
    {

        InitializeComponent();
        topp = new ObservableCollection<ClassMM>();
        startindex=dv=1;
        topp.Add(new ClassMM() {  PKId=dv, Name = "Test 1" });
        dv=2;
        topp.Add(new ClassMM() { PKId = dv, Name = "Test 2" });
        dv = 3;
        topp.Add(new ClassMM() { PKId = dv, Name = "Test 3" });

        dv = 4;
        topp.Add(new ClassMM() { PKId = dv, Name = "Test 4" });

        lastindex=dv = 5;
        topp.Add(new ClassMM() { PKId = dv, Name = "Test 5" });


    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        lastindex = dv = dv++;

        topp.Add(new ClassMM() { PKId = dv, Name =  musavebutton.Content.ToString() });
        foreach (var jk in topp.ToList())
        {
            MessageBox.Show(jk.Name);
        }
    }
     public class ClassMM : INotifyPropertyChanged
{
    public string _name;
    public int _pkid;


    public int PKId
    {
        get { return _pkid; }
        set
        {
            if (value != _pkid)
            {
                _pkid = value;
                NotifyPropertyChanged();
            }
        }
    }



    public string Name
    {
        get { return _name; }
        set
        {
            if (value != _name)
            {
                _name = value;
                NotifyPropertyChanged();
            }
        }
    }



    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
     }
   }

}

2 个答案:

答案 0 :(得分:3)

这是不正确的:ItemsSource="{Binding topp, Mode=TwoWay}"TwoWay与获取和设置绑定属性本身有关,在本例中为topp,而不是列表的内容。 ObservableList处理项目添加/删除通知。在这种情况下,您不希望项控件混淆topp的值,因此正确的绑定只是{Binding topp}

答案 1 :(得分:2)

将您的XAML保留为原始版本并按如下所示修改您的cs:

 public ObservableCollection<ClassMM> topp { get; set; }

        private int dv, startindex, lastindex;

        public MainWindow()
        {

            InitializeComponent();
            DataContext = this;
            topp = new ObservableCollection<ClassMM>();
            startindex = dv = 1;
            topp.Add(new ClassMM() {PKId = dv, Name = "Test 1"});
            dv = 2;
            topp.Add(new ClassMM() {PKId = dv, Name = "Test 2"});
            dv = 3;
            topp.Add(new ClassMM() {PKId = dv, Name = "Test 3"});

            dv = 4;
            topp.Add(new ClassMM() {PKId = dv, Name = "Test 4"});

            lastindex = dv = 5;
            topp.Add(new ClassMM() {PKId = dv, Name = "Test 5"});


        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            lastindex = dv = dv++;

            topp.Add(new ClassMM() { PKId = dv, Name = musavebutton.Content.ToString() });
            foreach (var jk in topp.ToList())
            {
                MessageBox.Show(jk.Name);
            }
        }

        public class ClassMM : INotifyPropertyChanged
        {
            public string _name;
            public int _pkid;


            public int PKId
            {
                get { return _pkid; }
                set
                {
                    if (value != _pkid)
                    {
                        _pkid = value;
                        NotifyPropertyChanged("PKId");
                    }
                }
            }



            public string Name
            {
                get { return _name; }
                set
                {
                    if (value != _name)
                    {
                        _name = value;
                        NotifyPropertyChanged("Name");
                    }
                }
            }



            public event PropertyChangedEventHandler PropertyChanged;

            protected void NotifyPropertyChanged(String propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }