我正在使用WPF tabcontrol来显示从 View 模型绑定(bind)的项目。

默认情况下,启动时会选择列表的第一项,但我不希望启动时选择任何项。我可以将OnSelectionChanged事件中的SelectedItem设置为null,然后在启动时没有选择任何项目,但是不再可以手动选择一个项目。

public partial class ProjectScopeMain : Window
{
  private bool firstStart = true;

  public ProjectScopeMain()
  {
    this.Initialized += this.ProjectScopeMain_Initialized;
    this.InitializeComponent();
  }

  private void ProjectScopeMain_Initialized(object sender, System.EventArgs e)
  {
    this.TabControlSettings.SelectionChanged += TabControlSettingsOnSelectionChanged;
  }

  private void TabControlSettingsOnSelectionChanged(object sender, EventArgs e)
  {
      this.TabControlSettings.SelectedItem = null;
  }

  private void ButtonCreate_Click(object sender, System.Windows.RoutedEventArgs e)
  {
    this.Close();
  }
}

我的XAML代码。 SelectedIndex = -1不起作用
        <customControls:TabControl x:Uid="tabControlSettings" x:Name="TabControlSettings"
                                   prism:RegionManager.RegionName="{x:Static infrastructure:RegionNames.ProjectScopeTabsRegion}"
                                   TabStripPlacement="Left" Style="{DynamicResource TabControlStyle}"

                                   ItemContainerStyle="{DynamicResource TabItemVerticalProjectScopeStyle}" SelectedIndex="-1"/>

最佳答案

我认为选项卡控件不会让您什么都没有选择。解决此问题的一个简单方法是创建一个具有可见折叠状态的空选项卡,并在您希望清除选项卡控件时导航到该选项卡。这将导致显示选项卡的内容(在这种情况下为空),并且没有标题。

<TabControl Name="MyTabControl" SelectedIndex="0">
    <TabItem Header="" Visibility="Collapsed">
        <!--There's nothing here-->
    </TabItem>
    <TabItem Header="Item 1">
        <TextBlock Text="Some item 1" />
    </TabItem>
    <TabItem Header="Item 2">
        <TextBlock Text="Some item 2" />
    </TabItem>
</TabControl>

您可以使用以下方法“清除”它:
MyTabControl.SelectedIndex = 0;

由于您希望绑定(bind)子项,因此我想您需要首先将子项合并到资源中。

关于c# - WPF TabControl启动时未选择任何项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37432066/

10-12 00:05
查看更多