0

I have ComboBox where I get my ItemsSource from my "Settings" object with the UserTypes ObservableCollection inside, and I need to bind its SelectedItem to my "Employee" object's type property. There are other fields within the window, which binds properly to other properties in the "Employee" object, the ItemsSource is correctly populated with the UserTypes property within Settings, and the only thing that is not working is the type property not binding with the combobo'x selecteditem, possibly because it's datacontext is set to the "Settings" object. Here is what I have so far:

<CollectionViewSource x:Key="settingsViewSource" d:DesignSource="{d:DesignInstance my:Settings, CreateList=True}" />
<CollectionViewSource x:Key="settingsUserTypesViewSource" Source="{Binding Path=UserTypes, Source={StaticResource settingsViewSource}}" />

<ComboBox DataContext="{StaticResource settingsUserTypesViewSource}" Name="userTypeBox" VerticalAlignment="Top" Width="100" Margin="2" ItemsSource="{Binding}"
SelectedItem="{Binding Path=Type, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">

In the code behind, I have:

        InitializeComponent();
        this.emp = emp;
        InfoTab.DataContext = emp;
        userTypeBox.DataContext = MainWindow.ViewSettings.UserTypes;

emp is a specific employee bound to the fields correctly, only the combo boxes' selecteditem is not working. Any help would be greatly appreciated.

Erika
  • 289
  • 1
  • 5
  • 20

1 Answers1

0

I don't know if you have a typo in the actual code or if it's just here, but you are missing a bracket

SelectedItem="Binding Path=Type, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"

should be

SelectedItem="{Binding Path=Type, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Botz3000
  • 39,020
  • 8
  • 103
  • 127
LadderLogic
  • 1,090
  • 9
  • 17
  • does your debug output print binding errors? It should be along the lines of "System.Windows.Data Error: 40 : BindingExpression path error:" – LadderLogic Jul 18 '12 at 21:36
  • System.Windows.Data Error: 40 : BindingExpression path error: 'UserType' property not found on 'object' ''ObservableCollection`1' (HashCode=50419575)'. BindingExpression:Path=UserType; DataItem='ObservableCollection`1' (HashCode=50419575); target element is 'ComboBox' (Name='userTypeBox'); target property is 'SelectedItem' (type 'Object') – Erika Jul 18 '12 at 21:43
  • by the way i have also changed the property name "Type" to "UserType" as suggested by Marc, though I changed everything else to UserType so I don't know what the problem is – Erika Jul 18 '12 at 21:44
  • That binding error tells the story. Its trying to set the SelectedItem property on your ObservableCollection, which obviously doesn't have a UserType property. This is because you set the DataContext to the Collection instead of the Item Source. Set your DataContext to your ViewModel and ItemSource to your static source. – LadderLogic Jul 18 '12 at 21:51
  • ItemsSource="{Binding Source={StaticResource settingsUserTypesViewSource} ...this is what I added instead and now the combobox is empty – Erika Jul 18 '12 at 21:54
  • 3
    post the answer here for future references. – LadderLogic Jul 18 '12 at 22:32