-1

I have a collection bound to Combobox and it's SelectedItem is bound to my viewmodel property SelectedItem.

<ComboBox ItemsSource="{Binding itemSource}"
          SelectedItem="{Binding SelectedItem}"/>

The class of SelectedItem is as follows:

public class SelectedItem
{
   public AnotherViewModel anotherViewModel {get;set;}
}

I have used an Usercontrol like below:

<local:usercontrol DataContext="{Binding SelectedItem.anotherViewModel}"/>

I am trying to change the content of usercontrol on selection change of combobox in main view.

The changes to properties of anotherViewModel reflects to the view only first time. On debugging the code, I found that properties of anotherViewModel contains new values but it doesn't reflect to view.

Any help would be appreciated.

Edit

Public class MainViewModel
{
   public string property1 {get;set;} //has propertychanged implemented
   public ObservableCollection<Item> Items {get;set;} //combobox itemsource
   public Item SelectedItem {get;set;}//combobox selecteditem  
}

public class Item
{ 
  public AnotherViewModel anotherViewModel {get;set;}//has propertychanged implemented
}

public class AnotherViewModel
{
  public string property1 {get;set;} //has propertychanged implemented
  public string property2 {get;set;} //has propertychanged implemented
  public ObservableCollection<string> items {get;set;} //has propertychanged implemented
}

<Window>
  <Textbox Text="{Binding property1}"/>
   <ComboBox ItemsSource="{Binding Items}"
          SelectedItem="{Binding SelectedItem}"/>
  <local:usercontrol DataContext="{Binding SelectedItem.anotherViewModel}"/>
<Window> 

<UserControl>
   <ListView ItemsSource="{Binding items}"/>
</UserControl>
Jayakrishnan
  • 4,232
  • 2
  • 23
  • 35
  • Does `SelectedItem` raise the `PropertyChanged` event? – mm8 May 27 '19 at 13:48
  • Yes. sorry I forgot to mention it. Every property has INotifyPropertyChanged implemented. JFYI, It was working fine in a single View until I created a new Usercontrol and moved the common code to it.. – Jayakrishnan May 27 '19 at 13:49
  • Please provide some more details. How is the `UserControl` defined for example? And the `SelectedItem` property? Are you setting this to a new `SelectedItem` property. – mm8 May 27 '19 at 13:50
  • @mm8, it's a simple WPF usercontrol having DataContext set. I haven't implemented any Dependency property... – Jayakrishnan May 27 '19 at 13:52
  • So the `UserControl` is empty? What do you expect to happen when you select an item? – mm8 May 27 '19 at 13:53
  • But I have set it's DataContext to my ViewModel i.e. SelectedItem.anotherViewModel. It works fine the first time but it doesn't work thereafter – Jayakrishnan May 27 '19 at 13:55
  • Do you probably explicitly set the UserControl's DataContext in its constructor or in its XAML? – Clemens May 27 '19 at 15:00
  • @Clemens No, I haven't set it on constructor. I know it overrides the chain. That's why I have set it only on Xaml in main view. – Jayakrishnan May 27 '19 at 15:22
  • Without more details your question can't be answered. Your code works fine for me. – Clemens May 28 '19 at 07:10

1 Answers1

0

You should not do a class SelectedItem encapsulating your AnotherViewModel class.

Rather, in the viewmodel that is the datacontext of your window you should have :

private AnotherViewModel _selectedItem;
public AnotherViewModel SelectedItem
    {
        get { return _selectedItem; }
        set { _selectedItem = value; OnPropertyChanged(nameof(SelectedItem)); }
    }

After that, you edit your xaml so that your usercontrol binds to your viewmodel property SelectedItem :

<local:usercontrol DataContext="{Binding SelectedItem}"/>

EDIT:

The standard structure would be :

  • The file MainView.xaml having your <Combobox> and a xaml element <local:usercontrol>.
  • The MainViewModel.cs having multiple properties, but especially the property SelectedItem of type AnotherViewModel and raising PropertyChangedEvent in its setter.
  • The AnotherViewModel.cs being anything you want. The goal is of course that your usercontrol inner xaml is binding to the properties you defined in AnotherViewModel class.
  • The usercontrol.xaml which is basically another view in which xaml elements are bound to AnotherViewModel properties.

And then you bind the Datacontext of your <local:usercontrol> to your MainViewModel SelectedItem property (as you did in your question).

vincenthavinh
  • 442
  • 4
  • 12
  • there are other properties in MainViewModel which I am using in MainView. AnotherViewModel is part of my MainViewModel. – Jayakrishnan May 27 '19 at 14:02
  • Can you give us more information about your MainViewModel and the declaration of your UserControl ? In your example, AnotherViewModel doesn't seems to be part of your MainViewModel – lema May 27 '19 at 14:06
  • 1
    I 'm having hard time figuring out your files structure. Do you have : - MainView with both the combobox and the in it - MainViewModel with a property "SelectedItem" and other properties. - AnotherViewModel with whatever properties in it If that's the case, there is no problem having other properties in your MainViewModel. What is important is that this MainViewModel has one property named SelectedItem pointing on the object AnotherViewModel. – vincenthavinh May 27 '19 at 14:06
  • @VincentHV, Please find the updated ViewModel structure. Thanks for your help – Jayakrishnan May 27 '19 at 14:24
  • I don't get what's wrong but your class Item is useless, you can just use your class AnotherViewModel instead. – lema May 27 '19 at 14:40
  • 1
    Agree with @lema, you class Item is not useful there. You can just put `AnotherViewModel` as the type of your MainViewModel `SelectedItem` property. And if you want to have different types of ViewModel in your combobox, an interface would be better. You define the Interface IItem, empty for the moment, then you put `IItem` as the type of your MainViewModel `SelectedItem` property, and then you make your class `AnotherViewModel` and the other viewmodel classes you want to show in combobox implement your interface `IItem`. I realize you have not populate your combobox in any way ? – vincenthavinh May 27 '19 at 14:45