0
votes

I'm using WPF TabControl and got a simple question (I hope).

I got a List of items, and for each item there are details displayed in a TabControl with 3 Tabs.

<TabControl Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="4" ItemsSource="{Binding SelectedLibrary.DetailViewModels}" IsSynchronizedWithCurrentItem="False">
  <TabControl.ItemTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding Name}" />
    </DataTemplate>
  </TabControl.ItemTemplate>

  <TabControl.Resources>
    <DataTemplate DataType="{x:Type provider:PropertyPageViewModel}">
      <dxprg:PropertyGridControl
        ReadOnly="True"
        ShowProperties="WithPropertyDefinitions"
        ExpandCategoriesWhenSelectedObjectChanged="True"
        ShowCategories="Hidden"
        ShowMenuButtonInRows="False"
        ShowToolPanel="False"
        ValueColumnWidth="2*"
        ShowSearchBox="False"
        SelectedObject="{Binding FunctionBaseData.Function, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
        <dxprg:PropertyDefinition Path="Name" Header="{x:Static fpProperties:Resources.PropertyGridHeaderName}" />
        <dxprg:PropertyDefinition Path="Id" Header="{x:Static fpProperties:Resources.PropertyGridHeaderId}"   />
        <dxprg:PropertyDefinition Path="Type" Header="{x:Static fpProperties:Resources.PropertyGridHeaderType}" />
        <dxprg:PropertyDefinition Path="Category" Header="{x:Static fpProperties:Resources.PropertyGridHeaderCategory}"  />
        <dxprg:PropertyDefinition Path="Version" Header="{x:Static fpProperties:Resources.PropertyGridHeaderVersion}"/>
        <dxprg:PropertyDefinition Path="LastModified" Header="{x:Static fpProperties:Resources.PropertyGridHeaderLastModified}" />
      </dxprg:PropertyGridControl>
    </DataTemplate>
    <DataTemplate DataType="{x:Type provider:PreviewPageViewModel}">
      <TextBlock Text="PreviewPage"></TextBlock>
    </DataTemplate>
    <DataTemplate DataType="{x:Type provider:CodePageViewModel}">
      <TextBlock Text="SourcePage"></TextBlock>
    </DataTemplate>
  </TabControl.Resources>
</TabControl>

This works.

When the user selects another item in the list the SelectedObject of my Tab changes, thats fine. But the selected tab also changes, I want the selected Tab to stay the same as for the item before.

I Tried this IsSynchronizedWithCurrentItem="True", but that wouldnt help. Please tell me theres a property I can easily set for the WPF TabControl.

Thanks

1
can you also provide your ViewModel code? - ViperLiu
There is SelectedObject in your sample markup. - mm8

1 Answers

0
votes

I don't think there is an XAML-only way to do this. All the "selected" properties (SelectedItem, SelectedIndex, etc.) will always reset when the ItemsSource changes.

You'll need to record the value of TabControl's SelectedIndex before the change, then set it back to that value after the change. Without seeing the rest of your code, I couldn't tell you exactly where to do those steps. The recording could be done in SelectedLibrary.DetailViewModels as part of the setter, or in whatever code that does the setting. Resetting the SelectedIndex could be done on SelectionChanged.