Binding a list<string> to ListBox

Page 1 of 1

7 Replies - 10474 Views - Last Post: 16 April 2013 - 03:03 PM Rate Topic: -----

#1 KFredje  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 06-September 11

Binding a list<string> to ListBox

Posted 09 April 2013 - 08:50 AM

Hello,

I try to bind a list<string> to my ListBox like I bind my other controls like TextBlocks but it doesn't seem to work out.
This is my code:
public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

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

        public List<string> ListStars
        {
            get { return _oListStars; }
            set
            {
                _oListStars = value;
                NotifyPropertyChanged("ListStars");
            }
        }

        List<string> _oListStars = new List<string>();

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            ListStars.Add("test");
        }
    }


                <ListBox Grid.Column="0"
                         Grid.Row="10"
                         Grid.ColumnSpan="2" 
                         Background="Transparent"
                         ItemsSource="{Binding Path=ListStars, Mode=OneWay}"/>


Is This A Good Question/Topic? 0
  • +

Replies To: Binding a list<string> to ListBox

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5678
  • View blog
  • Posts: 12,214
  • Joined: 02-June 10

Re: Binding a list<string> to ListBox

Posted 09 April 2013 - 11:16 AM

"It doesn't work"... What does that tell us?
Is the Listbox not showing the items on program launch? Is it not updating if the list updates? Is it not throwing an event when an item is selected?

You do realize that just adding an item to your List<> does not actually change the property, right? Unless you assign the property a new value which would be a completely new List<string> the property doesn't change.

Think of it this way. If you had an int property when you say int four = 4, then you are assigning a new int object to the property four. But if you just add a new string WITHIN the List<string> the object itself doesn't get replaced.

You probably want to look at observable collection.
http://msdn.microsof...y/ms668604.aspx

To prove to yourself that it doesn't change, put a breakpoint at the first line of the set method within the property. Notice it only changes when you say ListStars = new List<string> but not when you add a string to ListStars.

You could create a method to raise the PropertyChanged event for you and fool the GUI into updating.

// Untested
void AddStar(string NewMember)
{
   ListStars.Add(NewMember);
   NotifyPropertyChanged("ListStars");// This is what the GUI binding really looks for: Notice that the property has changed
}

Was This Post Helpful? 0
  • +
  • -

#3 KFredje  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 06-September 11

Re: Binding a list<string> to ListBox

Posted 09 April 2013 - 02:02 PM

That doesn't seem to work out. I noticed I can add TextBlocks to the list by dragging them into it in the visual editer. Then if I run the program they appear like I want the lol to be. Isn't it possible to just add strings to a ListBox in WPF like you do in C# or do you need something like a TextBlock at each line with a string in the TextBlock?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5678
  • View blog
  • Posts: 12,214
  • Joined: 02-June 10

Re: Binding a list<string> to ListBox

Posted 09 April 2013 - 02:25 PM

You can't add strings to the listbox in Designer *AND* bind the source to an object during runtime. That would basically be trying to assign two sources at the same time. Pick one or the other.
Was This Post Helpful? 0
  • +
  • -

#5 KFredje  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 06-September 11

Re: Binding a list<string> to ListBox

Posted 10 April 2013 - 07:29 AM

Well, then I'm confused. My goal is to show 3 strings on my form and you have to be able to click on them. A little example:

First you have the category like Food with 3 different types of food under it like fruit, chicken and meat. The 3 types of food should be like this on my form:

fruit, chicken and meat

When you hover a word you should be able to click it.

So, what is the easiest way to do this? A little guidance would be great here. (My first goal was to load each type of fruit on a line in a ListBox but I like my 2nd idea more if it's possible. Also the number of types should be variable.
Was This Post Helpful? 0
  • +
  • -

#6 KFredje  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 06-September 11

Re: Binding a list<string> to ListBox

Posted 13 April 2013 - 07:22 AM

bump?
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Binding a list<string> to ListBox

Posted 16 April 2013 - 01:58 PM

For your previous code to work, you should change how you are binding. First, you are binding to the property, but calling the .Add method on a list doesn't actually call the 'setter' of that property, which means it's not calling the NotifyPropertyChanged method. So you could change it to this...

<ListBox 
         x:Name="MyListBox"
         Grid.Column="0"
         Grid.Row="10"
         Grid.ColumnSpan="2" 
         Background="Transparent"
         ItemsSource="{Binding}"/>



public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

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

        ObservableCollection<string> _oListStars = new ObservableCollection<string>();

        private void Window_Loaded(object sender, EventArgs e)
        {
            _oListStars.Add("fruit");
            _oListStars.Add("chicken");
            _oListStars.Add("meat");
            MyListBox.ItemsSource = _oListStars;
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            _oListStars.Add("test");
        }
    }



The ObservableCollection implements the INotifyPropertyChanged interface, which will notify any control that it's bound to that it's items list has changed.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5678
  • View blog
  • Posts: 12,214
  • Joined: 02-June 10

Re: Binding a list<string> to ListBox

Posted 16 April 2013 - 03:03 PM

View Posteclipsed4utoo, on 16 April 2013 - 02:58 PM, said:

For your previous code to work, you should change how you are binding. First, you are binding to the property, but calling the .Add method on a list doesn't actually call the 'setter' of that property, which means it's not calling the NotifyPropertyChanged method. So you could change it to this...

The ObservableCollection implements the INotifyPropertyChanged interface, which will notify any control that it's bound to that it's items list has changed.


At this point all we are doing is repeating the same advice

View PosttlhIn`toq, on 09 April 2013 - 12:16 PM, said:

You do realize that just adding an item to your List<> does not actually change the property, right? Unless you assign the property a new value which would be a completely new List<string> the property doesn't change.

Think of it this way. If you had an int property when you say int four = 4, then you are assigning a new int object to the property four. But if you just add a new string WITHIN the List<string> the object itself doesn't get replaced.

You probably want to look at observable collection.
http://msdn.microsof...y/ms668604.aspx


The OP doesn't seem to be adding any new effort other than to "bump" the thread in the hopes that someone here writes their code for them. Which Elclipse was nice enough to do.

But at this point I think the ball is in the OP's court. If they aren't going to show any more interest or effort than what we've seen thus far I think the thread should be closed.

This post has been edited by tlhIn`toq: 16 April 2013 - 03:04 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1