1

I have been fighting with this for three days and I am about ready to smash my computer. Sorry, had to vent.

I have a WPF 4.5 application following the MVVM pattern with Entity Framework 6 as the model. In my application there is a Tab control in the main application window which contains a view/viewmodel for each tab.

The tab I am having trouble with is a simple master and detail view with a listbox for the list and a grid for the detail. The grid contains textboxes, comboboxes, and datepickers that are bound to the properties of the listbox's selected item. Everything works great when this view is first created and accessed by the user. But when I click on another tab and click back into this tab I get the following problem.

My Problem: Once another tab gets the focus and then the focus is returned to this tab, there two combo boxes that start to have binding issues.

The problem is not consistent, but for some reason the comboboxes start displaying incorrect values for the records. When I click through the listbox selecting different records, the comboboxes start to display wrong values and the binding is wrong as well in the viewmodel. But there are 10 other comboboxes and many more textboxes in the grid that display the right value for their respective properties without any errors.

I can't figure out why these two comboboxes start displaying the wrong values once the view's tab gets it's focus back from another tab.

Here is the declaration for the two comboboxes that are getting incorrect values, that selected item of the listbox is bound to the Selection property in the viewmodel:

<ContentControl Name="Selection" DataContext="{Binding Path=Selection, Mode=TwoWay}">
    <Grid >
        <ComboBox Name="idForeman" 
              Style="{StaticResource FTC_DetailComboBox}"
              ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.EmployeeForemanCVS.View}"
              SelectedValuePath="idEmployee"
              SelectedValue="{Binding idForeman, Mode=TwoWay, ValidatesOnDataErrors=True}"  >
            <ComboBox.ItemTemplate>
            ....
            </ComboBox.ItemTemplate>
        </ComboBox>

        <ComboBox Name="idClient"  
              Style="{StaticResource FTC_DetailComboBox}"
              ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.ClientViewSource.View}"
              SelectedValuePath="idClient"
              SelectedValue="{Binding idClient, Mode=TwoWay, ValidatesOnDataErrors=True}"
              DisplayMemberPath="chrCompany" >
            </ComboBox>
    <Grid />
<ContentControl />

Here is an example of a combobox that does not ever have an error, but works properly every time:

<ComboBox Name="idEnteredBy" 
      Style="{StaticResource FTC_DetailComboBox}"
      ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.EmployeeEnteredCVS.View}"
      SelectedValuePath="idEmployee"
      SelectedValue="{Binding idEnteredBy, Mode=TwoWay, ValidatesOnDataErrors=True}" >
    <ComboBox.ItemTemplate>
    ....
    </ComboBox.ItemTemplate>
</ComboBox>

As you can see they are declared the same way to the same type of item source.

All these controls are bound to a collectionViewSource that has an observableCollection for their source. The collections are created during the ViewModel's constructor.

QUESTION:

Can someone help me figure out why these comboboxes are getting wrong values only after their parent tab gets focus from another tab, but not when the tab is first created?

I know this is bad form, but I am totally desperate for a solution here as this is an application breaking bug. Any help is greatly appreciated.

Thanks in advance!

EDIT #1:

I added the datacontext declaration for the first xaml example above to the clarify where the comboboxes get thier context from.

J King
  • 4,108
  • 10
  • 53
  • 103
  • I don't quite understand the bindings of your Comboboxes. Usually your ListBox would be bound to Collection in ViewModel (probably an ObservableCollection). The SelectedItem of ListBox bound to an object in VM (call it SelectedItem) with Mode set to TwoWay and then the ComboBoxes bound to a List of SelectedItem eg SelectedItem.MyComboList. Its certainly seems like something to do with updating the viewmodel as your initial constructor puts the correct data in. – SWilko Jan 16 '14 at 10:35
  • @dellywheel , I updated my xaml to show that it is happening like you mention, with the selected item bound to a property called selection in the view model, then a content control that contains the comboboxes in question gets it datacontext set to the Selection property – J King Jan 16 '14 at 15:53

0 Answers0