Selected Category and Item binding

2 posts, 0 answers
  1. Daniel
    Daniel avatar
    4 posts
    Member since:
    Aug 2010

    Posted 15 Jul 2011 Link to this post

    I'm using the RadPanelBar to present a number of Categories, each with a set of Items within them.  The user can then expand a category and select an item within it.

    The SelectedItem is bound to the ViewModel, but this is set regardless of whether a Header (Category) is selected or an item under the header.  This means that the target in the ViewModel either raises binding exceptions or has to be weakly-typed (e.g. of type Object).

    I have other controls on-screen that need to react to the changes in the selected item, is there a way to separate out "HeaderSelected" and "ItemSelected" events?

    Thanks!
  2. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1511 posts

    Posted 20 Jul 2011 Link to this post

    Hi Daniel,

    The only way to achieve this functionality is to control it from the ViewModel. What I mean is the following.
    Imagine you have an object SelectedItem property in the ViewModel. You will have to notify the rest of the controls that the SelectedItem has changed only if it of type Category. Something like this:

    public class ViewModel : INotifyPropertyChanged
    {
        private object selectedItem;
     
        public object SelectedItem
        {
            get
            {
                return this.selectedItem;
            }
            set
            {
                if (this.selectedItem != value)
                {
                    this.selectedItem = value;
     
                    // Notify that the SelectedItem has changed only if it of type Category
                    if (this.selectedItem is Category)
                    {
                        this.OnPropertyChanged("SelectedItem");
                    }
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
     
        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    This means that you're correct when saying that the SelectedItem has to be of type object and not Category.

    In my opinion this is the most viable option in this particular scenario. Let me know what you thing about it. I'd be glad to further continue the discussion with you.

    Kind regards,
    Kiril Stanoev
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

  3. DevCraft banner
Back to Top