WPF TabControl no selected Item on start

情到浓时终转凉″ 提交于 2021-02-11 02:01:07

问题


I am using a WPF tabcontrol to display items which are bound from a viewmodel.

By default on start the first item of the list is selected but I want no item to be selected on start. I can set the SelectedItem in the OnSelectionChanged event to null then no item is selected on start but then it is no longer possible to manually select a item.

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();
  }
}

My XAML Code. SelectedIndex=-1 does not work

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

回答1:


I don't believe the tab control lets you have nothing selected. An easy work around for this is to create an empty tab with a collapsed visibility, and navigate to it when you would otherwise wish to clear your tab control. This will result in a tab's content being shown (which in this case is empty) and no header being present.

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

You could 'clear' it with:

MyTabControl.SelectedIndex = 0;

Since you wish to bind the child items, I would imagine you will need to combine the children in a resource first.




回答2:


You can deselect any TabItem by setting its IsSelected property to false. The content of TabControl will be blank once none of its TabItems are selected.



来源:https://stackoverflow.com/questions/37432066/wpf-tabcontrol-no-selected-item-on-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!