WPF TabControl 启动时没有选定的项目 [英] WPF TabControl no selected Item on start

查看:32
本文介绍了WPF TabControl 启动时没有选定的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WPF 选项卡控件来显示从视图模型绑定的项目.

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

默认情况下,在开始时选择列表的第一项,但我不希望在开始时选择任何项目.我可以将 OnSelectionChanged 事件中的 SelectedItem 设置为 null,然后在开始时不会选择任何项目,但随后无法手动选择项目.

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

我的 XAML 代码.SelectedIndex=-1 不起作用

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

推荐答案

我认为选项卡控件不会让您选择任何内容.一个简单的解决方法是创建一个具有折叠可见性的空选项卡,并在您希望清除选项卡控件时导航到它.这将导致显示选项卡的内容(在本例中为空)并且不存在标题.

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>

你可以清除"它:

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.

这篇关于WPF TabControl 启动时没有选定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆