1

I have two combo boxes. The first one, Companys has a collection of Company objects. Each object has a string Name and string collection:

Here is the class:

public class clsCompany : PropertyChangedBase
{
    public string Name { get; set; }
    public BindableCollection<string> Regions;
    public override string ToString()
    {
        return Name;
    }
}

Here is the code. The TextBox updates just fine when I select an item. But the ComboBox which should show the regions does not:

<ComboBox x:Name="Companys" DisplayMemberPath="Name" Width="100"/>
<ComboBox  x:Name="SelectedCompany_Regions" Width="100"/>
<TextBlock x:Name="SelectedCompany_Name" Width="100" />

The only way I can get the 2nd ComboBox to work is to create a new property on my ViewModel which is associated with SelectedCompany.Regions But why does what I have not work?

2 Answers 2

0

Your issue is caused by missing property changed notifications. When you update the Company you also need to raise a property changed notification for that companies Regions so that the regions ComboBox is aware its source has also updated. You can achieve this by changing the Companies and SelectedCompany properties to the following:

public BindableCollection<Company> Companys { get; set; }
private Company _selectedCompany;
public Company SelectedCompany
{
    get { return _selectedCompany; }
    set
    {
        _selectedCompany = value;
        NotifyOfPropertyChange(() => SelectedCompany);
        NotifyOfPropertyChange(() => SelectedCompany.Regions);
    }
}
4
  • I did exactly this, but still no update to the SelectedCompany_Regions combobox. May 24, 2011 at 15:19
  • @Daniel Hmmmmm...heres a sample application that works for this situation as I understand it...docs.google.com/…
    – Simon Fox
    May 24, 2011 at 19:58
  • Thank you- my Regions was a field, not a property, in the Company class. That was the issue. May 24, 2011 at 22:55
  • ah yes, the ViewModelBinder only searches properties
    – Simon Fox
    May 24, 2011 at 23:05
0

If you make Companys a CollectionView then you should be able to bind the second combo to Companys.CurrentItem.Regions.

2
  • Please explain how this answers the question. A code sample may be helpful.
    – this.josh
    May 18, 2011 at 17:36
  • Companys is a BindableCollection - what is a CollectionView? May 19, 2011 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.