Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In XAML:
HTML
<ComboBox x:Uid="ComboBox_1" ItemsSource="{Binding Path=Companies}" SelectedItem="{Binding Path=Company}"/>


In ViewModel:
public ObservableCollection<string> Companies { get; private set; }
public string Company{ get; private set; }

And I will add the single Company to Companies list in ViewModel constructor

But it doesn't work

the whole XAML will crash.

Not sure why....
Posted

1 solution

1. The SelectedItem where you bind should not have a private set.

Sample Solution:

ViewModel:
XML
public class CMainWindowViewModel
 {
     public ObservableCollection<string> Companies { get; private set; }


     public string Company { get;  set; }


     public CMainWindowViewModel()
     {
         Companies = new ObservableCollection<string>();
         Companies.Add("TestString");
     }
 }


Sample View:
XML
<Grid>
    <ComboBox x:Uid="ComboBox_1" ItemsSource="{Binding Path=Companies}" SelectedItem="{Binding Path=Company}"/>
</Grid>
 
Share this answer
 
Comments
allen_liu 25-Jul-13 2:55am    
Yes, correct ! thank you very much
stibee 25-Jul-13 4:03am    
Pls vote and mark it as solved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900