This is a migrated thread and some comments may be shown as answers.

RadListBox highlight item from MVVM

1 Answer 160 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Vladimir
Top achievements
Rank 1
Vladimir asked on 08 Apr 2013, 11:42 AM
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

1 Answer, 1 is accepted

Sort by
0
Vladimir
Top achievements
Rank 1
answered on 11 Apr 2013, 09:23 AM
Hi

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

Thanks 

Vitalij

Tags
ListBox
Asked by
Vladimir
Top achievements
Rank 1
Answers by
Vladimir
Top achievements
Rank 1
Share this question
or