Skip to content

Commit

Permalink
Add narration support to NavigationViewItem
Browse files Browse the repository at this point in the history
  • Loading branch information
Difegue committed Sep 24, 2024
1 parent 7969f43 commit 7f685c7
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Wpf.Ui/Controls/NavigationView/NavigationViewItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Input;
using Wpf.Ui.Controls.NavigationView;

Check failure on line 14 in src/Wpf.Ui/Controls/NavigationView/NavigationViewItem.cs

View workflow job for this annotation

GitHub Actions / build

A 'using namespace' directive can only be applied to namespaces; 'NavigationView' is a type not a namespace. Consider a 'using static' directive instead

Check failure on line 14 in src/Wpf.Ui/Controls/NavigationView/NavigationViewItem.cs

View workflow job for this annotation

GitHub Actions / build

A 'using namespace' directive can only be applied to namespaces; 'NavigationView' is a type not a namespace. Consider a 'using static' directive instead

// ReSharper disable once CheckNamespace
namespace Wpf.Ui.Controls;
Expand Down Expand Up @@ -438,6 +440,11 @@ protected override void OnMouseDown(MouseButtonEventArgs e)
e.Handled = true;
}

protected override AutomationPeer OnCreateAutomationPeer()
{
return new NavigationViewItemAutomationPeer(this);
}

private void OnMenuItems_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
SetValue(HasMenuItemsPropertyKey, MenuItems.Count > 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using System.Windows.Automation;
using System.Windows.Automation.Peers;

namespace Wpf.Ui.Controls;

internal class NavigationViewItemAutomationPeer : FrameworkElementAutomationPeer
{
private readonly NavigationViewItem _owner;

public NavigationViewItemAutomationPeer(NavigationViewItem owner)
: base(owner)
{
_owner = owner;
}

protected override string GetClassNameCore()
{
return "NavigationItem";
}

protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.TabItem;
}

public override object GetPattern(PatternInterface patternInterface)
{
if (patternInterface == PatternInterface.ItemContainer)
{
return this;
}

return base.GetPattern(patternInterface);
}

protected override AutomationPeer GetLabeledByCore()
{
if (_owner.Content is UIElement element)
{
return CreatePeerForElement(element);
}

return base.GetLabeledByCore();
}

protected override string GetNameCore()
{
var result = base.GetNameCore() ?? string.Empty;

if (result == string.Empty)
{
result = AutomationProperties.GetName(_owner);
}

if (result == string.Empty && _owner.Content is DependencyObject d)
{
result = AutomationProperties.GetName(d);
}

if (result == string.Empty && _owner.Content is string s)
{
result = s;
}

return result;
}
}

0 comments on commit 7f685c7

Please sign in to comment.