microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.34k stars 677 forks source link

TabView: Closing left-most tab throws exception when IsAddTabButtonVisible = false #3849

Open JsonTextfield opened 3 years ago

JsonTextfield commented 3 years ago

Describe the bug

When closing the left-most tab, calling sender.TabItems.Remove(args.Tab); in the TabCloseRequested event handler while IsAddTabButtonVisible = false causes a System.ArgumentException.

Steps to reproduce the bug

  1. Create a TabView with some tabs, set IsAddTabButtonVisible = false and create an event handler for TabCloseRequested.
  2. In the TabCloseRequested event handler, call sender.TabItems.Remove(args.Tab);.
  3. Run the app, select the left-most tab, and press the X button to close it.
    <muxc:TabView
            x:Name="MainTabView"
            Width="1490"
            Height="980"
            HorizontalAlignment="Left"
            VerticalAlignment="Center"
            IsAddTabButtonVisible="False"
            TabCloseRequested="MainTabView_TabCloseRequested">
            <muxc:TabViewItem Header="1" />
            <muxc:TabViewItem Header="2" />
            <muxc:TabViewItem Header="3" />
            <muxc:TabViewItem Header="4" />
    </muxc:TabView>
    private void MainTabView_TabCloseRequested(MUXC.TabView sender, MUXC.TabViewTabCloseRequestedEventArgs args)
    {
    sender.TabItems.Remove(args.Tab);
    }

Expected behavior Expected the left-most tab to close when its X button is pressed. Currently, an exception is thrown instead:

System.ArgumentException
  HResult=0x80070057
  Message=The parameter is incorrect.

The parameter is incorrect.

  Source=System.Private.CoreLib
  StackTrace:
   at System.Runtime.InteropServices.WindowsRuntime.IVector`1.RemoveAt(UInt32 index)
   at System.Runtime.InteropServices.WindowsRuntime.VectorToListAdapter.RemoveAtHelper[T](IVector`1 _this, UInt32 index)
   at System.Runtime.InteropServices.WindowsRuntime.VectorToCollectionAdapter.Remove[T](T item)
   at App1.MainPage.Tabs_TabCloseRequested(TabView sender, TabViewTabCloseRequestedEventArgs args) in \App1\MainPage.xaml.cs:line 39

Screenshots

Version Info

NuGet package version: Microsoft.UI.Xaml 2.5.0

Windows app type: UWP Win32
Windows 10 version Saw the problem?
Insider Build (xxxxx)
May 2020 Update (19041) Yes
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT

Additional context Current workaround is to set IsAddTabButtonVisible = true before calling sender.TabItems.Remove(args.Tab);:

private void MainTabView_TabCloseRequested(MUXC.TabView sender, MUXC.TabViewTabCloseRequestedEventArgs args)
{
    sender.IsAddTabButtonVisible = true;
    sender.TabItems.Remove(args.Tab);
    sender.IsAddTabButtonVisible = false;
}
dse-copsfs commented 1 year ago

We observed the same behaviour. In our case, it happens when there are 2 last tab items and we close the left-most tab.

Version Info

Version
Microsoft.NETCore.UniversalWindowsPlatform 6.2.14 (currently, the latest stable)
Microsoft.UI.Xaml 2.8.2 (currently, the latest stable)
Windows 10 Pro 22H2 (Windows Feature Experience Pack 120.2212.4190.0)