-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Work around dotnet/wpf#122 in Document Outline
- Loading branch information
Showing
2 changed files
with
52 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/VisualStudio/Core/Def/DocumentOutline/VirtualizingTreeView.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Windows.Automation.Peers; | ||
using System.Windows.Controls; | ||
|
||
namespace Microsoft.VisualStudio.LanguageServices.DocumentOutline; | ||
|
||
// Provide a workaround for https://github.com/dotnet/wpf/issues/122 when using virtualized TreeView. | ||
internal sealed class VirtualizingTreeView : TreeView | ||
{ | ||
public VirtualizingTreeView() | ||
{ | ||
// Two important properties are being set for TreeView: | ||
// We set IsVirtualizing to "true" so that WPF only generates internal data-structures elements that are visible. | ||
// Setting VirtualizationMode to "Recycling" ensures that WPF internal data is reused as items scroll in and out of view. | ||
VirtualizingPanel.SetIsVirtualizing(this, true); | ||
VirtualizingPanel.SetVirtualizationMode(this, VirtualizationMode.Recycling); | ||
} | ||
|
||
protected override AutomationPeer OnCreateAutomationPeer() | ||
=> new VirtualizingTreeViewAutomationPeer(this); | ||
|
||
public sealed class VirtualizingTreeViewAutomationPeer(TreeView owner) | ||
: TreeViewAutomationPeer(owner) | ||
{ | ||
protected override ItemAutomationPeer CreateItemAutomationPeer(object item) | ||
=> new VirtualizingTreeViewDataItemAutomationPeer(item, this, null); | ||
} | ||
|
||
public sealed class VirtualizingTreeViewDataItemAutomationPeer(object item, ItemsControlAutomationPeer itemsControlAutomationPeer, TreeViewDataItemAutomationPeer? parentDataItemAutomationPeer) | ||
: TreeViewDataItemAutomationPeer(item, itemsControlAutomationPeer, parentDataItemAutomationPeer) | ||
{ | ||
protected override string GetNameCore() | ||
{ | ||
try | ||
{ | ||
return base.GetNameCore(); | ||
} | ||
catch (NullReferenceException) | ||
{ | ||
// https://github.com/dotnet/wpf/issues/122 | ||
return ""; | ||
} | ||
} | ||
} | ||
} |