зеркало из https://github.com/dotnet/winforms.git
8768 :: Resolve Addrange issue after sorting. (#11423)
* 8768 :: Resolve Addrange issue after sorting.
This commit is contained in:
Родитель
fc564169d5
Коммит
5cefc3d5dc
|
@ -24,6 +24,7 @@ internal static partial class LocalAppContextSwitches
|
|||
internal const string DataGridViewUIAStartRowCountAtZeroSwitchName = "System.Windows.Forms.DataGridViewUIAStartRowCountAtZero";
|
||||
internal const string NoClientNotificationsSwitchName = "Switch.System.Windows.Forms.AccessibleObject.NoClientNotifications";
|
||||
internal const string EnableMsoComponentManagerSwitchName = "Switch.System.Windows.Forms.EnableMsoComponentManager";
|
||||
internal const string TreeNodeCollectionAddRangeRespectsSortOrderSwitchName = "System.Windows.Forms.TreeNodeCollectionAddRangeRespectsSortOrder";
|
||||
|
||||
private static int s_scaleTopLevelFormMinMaxSizeForDpi;
|
||||
private static int s_anchorLayoutV2;
|
||||
|
@ -34,6 +35,7 @@ internal static partial class LocalAppContextSwitches
|
|||
private static int s_dataGridViewUIAStartRowCountAtZero;
|
||||
private static int s_noClientNotifications;
|
||||
private static int s_enableMsoComponentManager;
|
||||
private static int s_treeNodeCollectionAddRangeRespectsSortOrder;
|
||||
|
||||
private static FrameworkName? s_targetFrameworkName;
|
||||
|
||||
|
@ -104,6 +106,11 @@ internal static partial class LocalAppContextSwitches
|
|||
return false;
|
||||
}
|
||||
|
||||
if (switchName == TreeNodeCollectionAddRangeRespectsSortOrderSwitchName)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (framework.Version.Major >= 8)
|
||||
{
|
||||
// Behavior changes added in .NET 8
|
||||
|
@ -202,6 +209,16 @@ internal static partial class LocalAppContextSwitches
|
|||
get => GetCachedSwitchValue(EnableMsoComponentManagerSwitchName, ref s_enableMsoComponentManager);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When set to (default), API will insert nodes in the sorted order.
|
||||
/// To get behavior compatible with the previous versions of .NET and .NET Framework, set this switch to.
|
||||
/// </summary>
|
||||
public static bool TreeNodeCollectionAddRangeRespectsSortOrder
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => GetCachedSwitchValue(TreeNodeCollectionAddRangeRespectsSortOrderSwitchName, ref s_treeNodeCollectionAddRangeRespectsSortOrder);
|
||||
}
|
||||
|
||||
internal static void SetLocalAppContextSwitchValue(string switchName, bool value)
|
||||
{
|
||||
if (switchName == NoClientNotificationsSwitchName)
|
||||
|
@ -218,6 +235,11 @@ internal static partial class LocalAppContextSwitches
|
|||
{
|
||||
s_applyParentFontToMenus = value ? 1 : 0;
|
||||
}
|
||||
|
||||
if (switchName == TreeNodeCollectionAddRangeRespectsSortOrderSwitchName)
|
||||
{
|
||||
s_treeNodeCollectionAddRangeRespectsSortOrder = value ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
internal static bool GetCachedSwitchValue(string switchName)
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
using System.Collections;
|
||||
using System.ComponentModel;
|
||||
using System.Drawing.Design;
|
||||
using System.Windows.Forms.Primitives;
|
||||
|
||||
namespace System.Windows.Forms;
|
||||
|
||||
|
@ -219,7 +220,11 @@ public class TreeNodeCollection : IList
|
|||
tv.BeginUpdate();
|
||||
}
|
||||
|
||||
_owner.Nodes.FixedIndex = _owner._childCount;
|
||||
if (!LocalAppContextSwitches.TreeNodeCollectionAddRangeRespectsSortOrder || tv is null || !tv.Sorted)
|
||||
{
|
||||
_owner.Nodes.FixedIndex = _owner._childCount;
|
||||
}
|
||||
|
||||
_owner.EnsureCapacity(nodes.Length);
|
||||
for (int i = nodes.Length - 1; i >= 0; i--)
|
||||
{
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms.Primitives;
|
||||
|
||||
namespace System.Windows.Forms.Tests;
|
||||
|
||||
|
@ -214,6 +215,119 @@ public class TreeNodeCollectionTests
|
|||
Assert.Empty(collection.Find(key, searchAllChildren: false));
|
||||
}
|
||||
|
||||
[WinFormsFact]
|
||||
public void TreeNodeCollection_Sort_ShouldAfterAddingItems()
|
||||
{
|
||||
using TreeView treeView = new();
|
||||
string key = "7";
|
||||
TreeNode child1 = new()
|
||||
{
|
||||
Name = "8",
|
||||
Text = "8"
|
||||
};
|
||||
TreeNode child2 = new()
|
||||
{
|
||||
Name = "5",
|
||||
Text = "5"
|
||||
};
|
||||
TreeNode child3 = new()
|
||||
{
|
||||
Name = "7",
|
||||
Text = "7"
|
||||
};
|
||||
|
||||
treeView.Nodes.Add(child1);
|
||||
treeView.Nodes.Add(child2);
|
||||
treeView.Nodes.Add(child3);
|
||||
|
||||
treeView.Sort();
|
||||
treeView.CreateControl();
|
||||
|
||||
TreeNode[] treeNodeArray = new TreeNode[]
|
||||
{
|
||||
new()
|
||||
{
|
||||
Name = "2",
|
||||
Text = "2"
|
||||
},
|
||||
new()
|
||||
{
|
||||
Name = "1",
|
||||
Text = "1"
|
||||
}
|
||||
};
|
||||
|
||||
treeView.Nodes.AddRange(treeNodeArray);
|
||||
|
||||
TreeNode treeNode = treeView.Nodes.Find(key, searchAllChildren: true)[0];
|
||||
treeNode.Should().NotBeNull();
|
||||
treeView.Nodes.IndexOf(treeNode).Should().Be(3);
|
||||
}
|
||||
|
||||
[WinFormsFact]
|
||||
public void TreeNodeCollection_TreeNodeCollectionAddRangeRespectsSortOrderSwitch()
|
||||
{
|
||||
TreeNode child1 = new()
|
||||
{
|
||||
Name = "8",
|
||||
Text = "8"
|
||||
};
|
||||
TreeNode child2 = new()
|
||||
{
|
||||
Name = "5",
|
||||
Text = "5"
|
||||
};
|
||||
TreeNode child3 = new()
|
||||
{
|
||||
Name = "7",
|
||||
Text = "7"
|
||||
};
|
||||
TreeNode[] treeNodeArray = new TreeNode[]
|
||||
{
|
||||
new()
|
||||
{
|
||||
Name = "2",
|
||||
Text = "2"
|
||||
},
|
||||
new()
|
||||
{
|
||||
Name = "1",
|
||||
Text = "1"
|
||||
}
|
||||
};
|
||||
|
||||
LocalAppContextSwitches.SetLocalAppContextSwitchValue(LocalAppContextSwitches.TreeNodeCollectionAddRangeRespectsSortOrderSwitchName, true);
|
||||
using TreeView treeView2 = new();
|
||||
|
||||
treeView2.Nodes.Add(child1);
|
||||
treeView2.Nodes.Add(child2);
|
||||
treeView2.Nodes.Add(child3);
|
||||
|
||||
treeView2.CreateControl();
|
||||
treeView2.Sort();
|
||||
treeView2.Nodes.AddRange(treeNodeArray);
|
||||
|
||||
TreeNode treeNode = treeView2.Nodes.Find("2", searchAllChildren: true)[0];
|
||||
treeNode.Should().NotBeNull();
|
||||
treeView2.Nodes.IndexOf(treeNode).Should().Be(1);
|
||||
treeView2.Nodes.Clear();
|
||||
|
||||
LocalAppContextSwitches.SetLocalAppContextSwitchValue(LocalAppContextSwitches.TreeNodeCollectionAddRangeRespectsSortOrderSwitchName, false);
|
||||
using TreeView treeView3 = new();
|
||||
|
||||
treeView3.Nodes.Add(child1);
|
||||
treeView3.Nodes.Add(child2);
|
||||
treeView3.Nodes.Add(child3);
|
||||
|
||||
treeView3.CreateControl();
|
||||
treeView3.Sort();
|
||||
treeView3.Nodes.AddRange(treeNodeArray);
|
||||
|
||||
treeNode = treeView3.Nodes.Find("2", searchAllChildren: true)[0];
|
||||
treeNode.Should().NotBeNull();
|
||||
treeView3.Nodes.IndexOf(treeNode).Should().Be(1);
|
||||
}
|
||||
|
||||
[WinFormsTheory]
|
||||
[NullAndEmptyStringData]
|
||||
public void TreeNodeCollection_Find_NullOrEmptyKey_ThrowsArgumentNullException(string key)
|
||||
|
|
Загрузка…
Ссылка в новой задаче