当SelectedObject发生变化时,WPF Tab控件会保留所选的标签。

问题描述 投票:0回答:1

我正在使用WPF TabControl,并得到了一个简单的问题(我希望)。

我得到了一个项目列表,每个项目的详细信息都显示在一个有3个Tab的TabControl中。

<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>

这样做是可行的。

当用户在列表中选择另一个项目时,该列表中的所有项目都会被显示出来。SelectedObject 我的Tab改变了,这很好,但是选择的Tab也改变了,我想让选择的Tab和之前的项目保持一致。

我试了一下 IsSynchronizedWithCurrentItem="True"但这也无济于事。请告诉我有一个属性,我可以很容易地设置为WPF标签控件。

谢谢。

wpf tabcontrol
1个回答
0
投票

我不认为有一个只用XAML的方法来做这件事。所有的 "选定 "属性(SelectedItem, SelectedIndex等)时,总是会复位。ItemsSource 的变化。

你需要记录 TabControl's SelectedIndex 变更前,然后在变更后将其设回该值。如果没有看到你的其他代码,我无法告诉你具体在哪里做这些步骤。 记录可以在 SelectedLibrary.DetailViewModels 作为设置器的一部分,或在任何进行设置的代码中。重新设置 SelectedIndex 可在 SelectionChanged.

© www.soinside.com 2019 - 2024. All rights reserved.