Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Bind ItemsSource with ObservableCollection in WPF

Tags:

c#

.net

wpf

xaml

In my WPF Application - I add new item to ObservableCollection via Button Click Event Handler. Now i want to show this added item immediately as it adds to ObservableCollection via Binding to ItemsControl I wrote code but it is not working. Can anyone solve my problem. Here code is:

.XAML File

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

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

}

like image 240
user2835256 Avatar asked Oct 06 '13 17:10

user2835256


People also ask

What is ObservableCollection in WPF?

WPF provides the ObservableCollection<T> class, which is a built-in implementation of a data collection that implements the INotifyCollectionChanged interface.

What is the difference between ObservableCollection and list?

One more important difference is you can access ObservableCollection only from thread on which it was created where as list can be accessed fromany thread. Save this answer. Show activity on this post. I see no problem with that, other than a very marginal performance overhead.

What is ItemsSource in WPF?

ItemsSource can be data bound to any sequence that implements the IEnumerable interface, although the type of collection used does determine the way in which the control is updated when items are added to or removed. When ItemsSource is set, the Items property cannot be used to control the displayed values.

What does binding DO WPF?

Data binding in Windows Presentation Foundation (WPF) provides a simple and consistent way for apps to present and interact with data. Elements can be bound to data from different kinds of data sources in the form of . NET objects and XML.


1 Answers

Keep your XAML as original and modify you cs as follows :

 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));
                }
            }
        }
like image 110
HichemSeeSharp Avatar answered Sep 22 '22 14:09

HichemSeeSharp