RadListBox highlight item from MVVM

2 posts, 0 answers
  1. Vitalij
    Vitalij avatar
    35 posts
    Member since:
    Dec 2010

    Posted 08 Apr 2013 Link to this post

    Hi

    I have one question. How can I highlight an item from ViewModel? I have simple MVVM scenario:

    public class FooViewModel : ViewModelBase
        {
            private ObservableCollection<Foo> _fooCollection;
            private Foo _selectedFoo;
            private ICommand _addCommand;
     
            public ObservableCollection<Foo> FooCollection
            {
                get { return _fooCollection; }
                set
                {
                    _fooCollection = value;
                    OnPropertyChanged("FooCollection");
                }
            }
     
            public Foo SelectedFoo
            {
                get { return _selectedFoo; }
                set
                {
                    _selectedFoo = value;
                    OnPropertyChanged("SelectedFoo");
                }
            }
     
            public ICommand AddCommand
            {
                get { return _addCommand ?? (_addCommand = new RelayCommand(i=>this.AddCommandMethod())); }
            }
     
            private void AddCommandMethod()
            {
                //Add item to collection
                //Add item to SelectedFoo
            }

    In View I bind ItemSource to FooCollection, I am binding the SelectedItem of the RadListBox to SelectedFoo from VM and I have two TextBlocks to display the SelectedFoo. I have one button to Add a new Item to the Collection and setting the SelectedFoo to this new Item from Collection. Everything is fine, the two textblocks shows correct data, but the RadListBox is not highlighting the new Item Added to the collection. The collection rebinds fine because I can see the added item in list, but it is not highlighted. 

    Am I doing something wrong?? I tried to set the SelectedValue, SelectedValuePath, SelectedItem. I have google it a lot but no luck.

    Thanks Vitalij
  2. Vitalij
    Vitalij avatar
    35 posts
    Member since:
    Dec 2010

    Posted 11 Apr 2013 Link to this post

    Hi

    Nevermind I figured it out. I had wrong binding to List and selectedItem.

    Thanks 

    Vitalij

  3. UI for WPF is Visual Studio 2017 Ready
Back to Top