View Model Binding to TabControl Items

Apr 25, 2012 at 1:28 PM
Edited May 7, 2012 at 10:04 AM

I've been having trouble with the following issue:

I have a main viewmodel that has a property with a list of data objects called Items and a SelectedItem property.  The main view has a DataGrid for showing the items and a TabControl containing two User Controls to show the details. Caliburn Micro has bound The DataGrid to the Items property and the selected item to the SelectedItem property of the view model. The user controls are both bound in xaml to the selected item, the text boxes inside the user controls are bound by Caliburn Micro to the belonging data properties. So far so good.

If I e.g. select the first ten items in the list one after one the following happens:

The user control in the visible tab item is updated after each change of the selection, this is good. When I select the second tab things go wrong. The SelectedItem property is set ten times, this happens in the order the items where selected. If I switch to the second tab the same happens. It event gets worse if a third tab is added. In this case the number of sets increases after each change of tab item. I have noticed that this only happens when there are multiple user controls in the tab control that have bindings to properties of the selected item.

In my main view model i have:

<TabControl Name="tabControl1">
    <TabItem Header="Data">
        <local:DataCaliburnView DataContext="{Binding SelectedItem, Mode=TwoWay}" />
    </TabItem>
    <TabItem Header="Second">
        <local:DataCaliburnView DataContext="{Binding SelectedItem, Mode=TwoWay}" />
    </TabItem>
    <TabItem Header="Sub Data">
        <local:SubDataCaliburnView DataContext="{Binding SelectedItem, Mode=TwoWay}" />
    </TabItem>
</TabControl>

<DataGrid Name="Items" />

The DataCaliburnView and SubDataCaliburnView are user controls containing tree textboxes that are bound by Caliburn Micro to properties of the DataViewModel / SubDataViewModel. The binding mode of the user controls is TwoWay because otherwise they are not updated after changing the selection in the grid. OneWay binding does work if the user controls are bound in xaml and not by Caliburn Micro. 

The data classes:

public class DataViewModel
{
    public int Id { get; set; }
    public int Number { get; set; }
    public string Text { get; set; }
    public DataSubViewModel SubView { get; set; }
}

public class DataSubViewModel
{
    public int Id { get; set; }
    public int Number { get; set; }
    public string Text { get; set; }
}

Can anyone help me solving this problem? 

Thanks in advance.

Coordinator
May 7, 2012 at 11:28 AM

Can you create a *small* sample solution that demonstrates *only* this issue and attach it to a ticket?

May 7, 2012 at 12:47 PM

I have created http://caliburnmicro.codeplex.com/workitem/226 with an sample solution attached to it.

Thanks in advance.

Jun 14, 2012 at 12:59 PM

anybody?

Sep 3, 2012 at 7:03 AM
Edited Sep 3, 2012 at 7:12 AM

The issue has been resolved, many thanks to tibel for helping me out with this one. See the work item for details.