1

I have a MVVM application with a MainWindowViewModel connected to a MainWindowview.
On the MainWindow view I have a ComboBox containing stock ticker symbols.

I have another viewmodel and view called AllStockQuoteViewModel connected to AllStockQuoteView which contains a list of stocks and their prices.

I want to be able to select an item from the ComboBox and have the item in the AllStockQuoteView selected and highlighted. On my MainWindowViewModel I have saved the reference to the AllStockQuoteViewModel and use it to call a method to find the stock ticker symbol in the ObservableCollection bound to the AllStockQuoteView, but haven't found a way to programmatically select the item on the AllStockQuoteView.

I have a SelectedQuote property on the AllStockQuoteViewModel bound to the listview on the AllStockQuoteView and I can select one of the items and my SelectedQuote property is set fine. If I set this programmatically in my SelectQuote method, it doesn't appear as if the item is selected in the view, although the item is passed back to the MainWindowViewModel and I can use it to populate the textblocks on MainWindow view.

I'd like to be able to show the item on the AllStockQuoteView as being selected via highlighting as if the user selected it.

How can this be done?

Miamy
  • 2,162
  • 3
  • 15
  • 32
JohnBlacker
  • 173
  • 2
  • 10

2 Answers2

3

Its very easy to implement

You need two things in your view model A List of your objects and a selected item property

        private CustomObject _selectedCustomObject;

        public ObservableCollection<CustomObject> CustomObjects
        {
            get
            {
                return new ObservableCollection<CustomObject>();
            }
        }

        public CustomObject SelectedCustomObject
        {
            get { return _selectedCustomObject; }
            set
            {
                if (_selectedCustomObject== value)
                {
                    return;
                }

                _selectedCustomObject= value;
                PropertyChanged.Raise(this, x => x.SelectedCustomObject);
            }
        }

Then in your view you add your List/Combo control and bind to both properties.

<ListView ItemsSource="{Binding CustomObjects}"
          SelectedItem="{Binding SelectedCustomObject}">

Then all you need to do is set the viewmodel properties and the view will update.

John Petrak
  • 2,898
  • 20
  • 31
  • The problem with the above code is that each time it retrieves the CustomObjects collection, it creates a new one destroying whatever was in it before...I end up with nothing to display... – JohnBlacker Mar 30 '12 at 00:10
  • Yeah, You can implement those two properties how you see fit. I used it for the sake of an example, you could return a private variable just like the selected item. (The original code I modified for this example, did a request from a model for an updated list from the data store instead of a static list). Your original question was mainly about the selected item. I failed to mention that whatever you assign to the selected item property must be in the list of object (reference match). – John Petrak Mar 30 '12 at 00:38
0

First you have to think about your model and the whole MVVM approach, a good starting point is http://blogs.msdn.com/b/kashiffl/archive/2010/11/14/mvvm-technical-description.aspx.

After you can implement your functionality by different ways, one would be to implement something like the Observer Pattern or you try to use methods like Notify Property-Changed-Events.

Hope i was able to help,

Greetings

Mario Fraiß
  • 1,095
  • 7
  • 15
  • Thanks, I'll check these items out and evaluate my design. But I still have the basic question of how can a MVVM viewmodel programmatically select an item in it's view? – JohnBlacker Mar 29 '12 at 23:08
  • Check this question out http://stackoverflow.com/questions/803216/managing-multiple-selections-with-mvvm - i think it will help you solving your problem. – Mario Fraiß Mar 30 '12 at 00:30