I have a problem with a UserControl I wrote, when I use it in a TabControl of our application. The TabControl is DataBound and uses Templates. On change of the current Tab (per MouseClick) a bound property is set to null. I would like to keep the currently selected item in the ViewModel. If I use a DataGrid or ListBox everything works as espected. The SelectedItem-Property of the ViewModel is not set to null.
Maybe I have to setup my DependencyProperty different? The involved properties are defined like this:
SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(BusinessListEditor), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(BusinessListEditor), new FrameworkPropertyMetadata(null));
This properties are directly bound to a DataGrid inside the UserControl.
<DataGrid x:Name="dgDataGrid"
ItemsSource="{Binding ItemsSource,
ElementName=ucBusinessListEditor}"
SelectedItem="{Binding SelectedItem,
ElementName=ucBusinessListEditor}"
Style="{Binding DataGridStyle,
ElementName=ucBusinessListEditor}" />
The question how I can prevent the SelectedItem-Property from changing on a SelectedTab-Change. I tried to analyse this but do not fully understand the case. Somehow the SelectedItemChanged comes from a ItemsChanged of the DataGrid.