locked
Working with a ListBox in Blend RRS feed

  • Question

  • Hey,

    I'm using a custom ListBox (it functions as a table - I edited the ItemTemplate to have different columns).
    I have 2 problems:

    1. I can't seem to get an event from a click on a row. I need to get the selected row index and I need to capture an event of a click on the ListBox.
    2. My ListBox is bound to an ObservableCollection of a cutom type. I have this method:
            public void AddRow()
            {
                rows.Add(new ExerciseRow("test", "test", "test", "test"));
                this.NotifyPropertyChanged("Rows");
            }
        I can only get a row to be added to the ListBox when I call AddRow() inside the constructor. However, when I call it somewhere else is just doesn't do         anything. The data binding of the ListBox is defined as OneWay and this is my NotifyPropertyChanged (It works with a string and a label)
            public event PropertyChangedEventHandler PropertyChanged;

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

    Thanks!

    Wednesday, May 21, 2008 12:03 PM

All replies

  • For your ListBoxItem, click on the "Properties" tab then the "Events" button [<>].  Scroll down to the "Selected" entry and type in the name of your function.  Hit "Enter" and then program in C# what you want that selection to trigger.
    - Chad
    Thursday, May 22, 2008 12:47 PM
  • Thank you,

    What about the data binding?

    Aviv
    Thursday, May 22, 2008 7:33 PM
  • If you're using XML, you can bind the elements in Blend within your ListBox's data template.  Deeper C# database data binding will have to be answered by someone else (sorry!).
    - Chad
    Thursday, May 22, 2008 8:28 PM
  • Thanks for your help though I'm binding to a CLR object and not an XML.

    Thanks again!
    Thursday, May 22, 2008 9:26 PM
  • Any idea guys? Just can't seem to find an answer..
    Sunday, May 25, 2008 5:13 PM
  • If you are binding to an ObservableCollection<T>, you don't need to do anything else to triggers UI updates - the data structure automatically implements INotifyCollectionChange (I think that is what the interface is called for collections, please doube check) that should cause UI to update whenever you add /remove items from the collection, constructor or not.

    If you could attache a simple repro of your application, I could take a look.

    Unni
    Tuesday, May 27, 2008 6:28 PM
    Moderator