![](/img/trans.png)
[英]WPF TabControl when changed selected item MediaUriElement hides
[英]WPF TabControl no selected Item on start
我正在使用 WPF tabcontrol 來顯示從視圖模型綁定的項目。
默認情況下,在開始時選擇列表的第一項,但我不希望在開始時選擇任何項目。 我可以將 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;
由於您希望綁定子項,我想您需要先將子項合並到一個資源中。
您可以通過將其IsSelected
屬性設置為false
來取消選擇任何TabItem
。 一旦沒有選擇TabItem
, TabControl
的內容將是空白的。
您可以通過設置 SelectedTab 屬性 nullptr 來選擇任何內容。
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.tabcontrol.selectedtab?view=net-5.0
一個 TabPage,代表選定的標簽頁。 如果沒有選擇標簽頁,則該值為空。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.