Skip to content

Commit d113a45

Browse files
committed
Menu and Menuitem should provide child items if grouping is active
As for ContextMenuAutomationPeer the same has to be done for MenuAutomationPeer and MenuItemAutomation peer. Fix #6530
1 parent 932c912 commit d113a45

File tree

4 files changed

+73
-10
lines changed

4 files changed

+73
-10
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Automation/Peers/UIElementAutomationPeer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ override protected void SetFocusCore()
540540
throw new InvalidOperationException(SR.Get(SRID.SetFocusFailed));
541541
}
542542

543-
private UIElement _owner;
543+
private readonly UIElement _owner;
544544
private SynchronizedInputAdaptor _synchronizedInputPattern;
545545
}
546546
}

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/MenuAutomationPeer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
namespace System.Windows.Automation.Peers
2222
{
2323
///
24-
public class MenuAutomationPeer : FrameworkElementAutomationPeer
24+
public class MenuAutomationPeer : ItemsControlAutomationPeer
2525
{
2626
///
2727
public MenuAutomationPeer(Menu owner): base(owner)
@@ -46,6 +46,11 @@ protected override bool IsContentElementCore()
4646
{
4747
return false;
4848
}
49+
50+
protected override ItemAutomationPeer CreateItemAutomationPeer(object item)
51+
{
52+
return new MenuItemDataAutomationPeer(item, this);
53+
}
4954
}
5055
}
5156

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/MenuItemAutomationPeer.cs

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@
2121
namespace System.Windows.Automation.Peers
2222
{
2323
///
24-
public class MenuItemAutomationPeer : FrameworkElementAutomationPeer, IExpandCollapseProvider, IInvokeProvider, IToggleProvider
24+
public class MenuItemAutomationPeer : ItemsControlAutomationPeer, IExpandCollapseProvider, IInvokeProvider, IToggleProvider
2525
{
2626
///
2727
public MenuItemAutomationPeer(MenuItem owner): base(owner)
2828
{
2929
}
3030

31+
override protected ItemAutomationPeer CreateItemAutomationPeer(object item)
32+
{
33+
return new MenuItemDataAutomationPeer(item, this);
34+
}
35+
3136
///
3237
override protected string GetClassNameCore()
3338
{
@@ -173,16 +178,14 @@ override protected string GetAccessKeyCore()
173178
///
174179
protected override List<AutomationPeer> GetChildrenCore()
175180
{
176-
List<AutomationPeer> children = base.GetChildrenCore();
177-
178181
if (ExpandCollapseState.Expanded == ((IExpandCollapseProvider)this).ExpandCollapseState)
179182
{
180183
ItemsControl owner = (ItemsControl)Owner;
181184
ItemCollection items = owner.Items;
182185

183186
if (items.Count > 0)
184187
{
185-
children = new List<AutomationPeer>(items.Count);
188+
List<AutomationPeer> children = new List<AutomationPeer>(items.Count);
186189
for (int i = 0; i < items.Count; i++)
187190
{
188191
UIElement uiElement = owner.ItemContainerGenerator.ContainerFromIndex(i) as UIElement;
@@ -195,12 +198,65 @@ protected override List<AutomationPeer> GetChildrenCore()
195198
children.Add(peer);
196199
}
197200
}
201+
202+
return children;
198203
}
199204
}
200205

206+
return GetChildrenFromVisualTree();
207+
}
208+
209+
private List<AutomationPeer> GetChildrenFromVisualTree()
210+
{
211+
List<AutomationPeer> children = null;
212+
213+
iterate(Owner, ref children);
201214
return children;
202215
}
203216

217+
private static bool AddPeerToList(AutomationPeer peer, ref List<AutomationPeer> children)
218+
{
219+
if (children == null)
220+
children = new List<AutomationPeer>();
221+
222+
children.Add(peer);
223+
return false;
224+
}
225+
226+
private static bool iterate(DependencyObject parent, ref List<AutomationPeer> children)
227+
{
228+
bool done = false;
229+
230+
if (parent != null)
231+
{
232+
AutomationPeer peer = null;
233+
int count = VisualTreeHelper.GetChildrenCount(parent);
234+
for (int i = 0; i < count && !done; i++)
235+
{
236+
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
237+
238+
if (child != null
239+
&& child is UIElement
240+
&& (peer = CreatePeerForElement((UIElement)child)) != null)
241+
{
242+
done = AddPeerToList(peer, ref children);
243+
}
244+
else if (child != null
245+
&& child is UIElement3D
246+
&& (peer = UIElement3DAutomationPeer.CreatePeerForElement(((UIElement3D)child))) != null)
247+
{
248+
done = AddPeerToList(peer, ref children);
249+
}
250+
else
251+
{
252+
done = iterate(child, ref children);
253+
}
254+
}
255+
}
256+
257+
return done;
258+
}
259+
204260
///
205261
void IExpandCollapseProvider.Expand()
206262
{

src/Microsoft.DotNet.Wpf/src/PresentationFramework/ref/PresentationFramework.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2886,12 +2886,13 @@ public MediaElementAutomationPeer(System.Windows.Controls.MediaElement owner) :
28862886
protected override System.Windows.Automation.Peers.AutomationControlType GetAutomationControlTypeCore() { throw null; }
28872887
protected override string GetClassNameCore() { throw null; }
28882888
}
2889-
public partial class MenuAutomationPeer : System.Windows.Automation.Peers.FrameworkElementAutomationPeer
2889+
public partial class MenuAutomationPeer : System.Windows.Automation.Peers.ItemsControlAutomationPeer
28902890
{
2891-
public MenuAutomationPeer(System.Windows.Controls.Menu owner) : base (default(System.Windows.FrameworkElement)) { }
2891+
public MenuAutomationPeer(System.Windows.Controls.Menu owner) : base (default(System.Windows.Controls.ItemsControl)) { }
28922892
protected override System.Windows.Automation.Peers.AutomationControlType GetAutomationControlTypeCore() { throw null; }
28932893
protected override string GetClassNameCore() { throw null; }
28942894
protected override bool IsContentElementCore() { throw null; }
2895+
override protected System.Windows.Automation.Peers.ItemAutomationPeer CreateItemAutomationPeer(object item) { throw null; }
28952896
}
28962897

28972898
public partial class MenuItemDataAutomationPeer : System.Windows.Automation.Peers.ItemAutomationPeer, System.Windows.Automation.Provider.IExpandCollapseProvider, System.Windows.Automation.Provider.IInvokeProvider, System.Windows.Automation.Provider.IToggleProvider
@@ -2907,11 +2908,12 @@ void System.Windows.Automation.Provider.IExpandCollapseProvider.Collapse() { }
29072908
void System.Windows.Automation.Provider.IToggleProvider.Toggle() { throw null; }
29082909
}
29092910

2910-
public partial class MenuItemAutomationPeer : System.Windows.Automation.Peers.FrameworkElementAutomationPeer, System.Windows.Automation.Provider.IExpandCollapseProvider, System.Windows.Automation.Provider.IInvokeProvider, System.Windows.Automation.Provider.IToggleProvider
2911+
public partial class MenuItemAutomationPeer : System.Windows.Automation.Peers.ItemsControlAutomationPeer, System.Windows.Automation.Provider.IExpandCollapseProvider, System.Windows.Automation.Provider.IInvokeProvider, System.Windows.Automation.Provider.IToggleProvider
29112912
{
2912-
public MenuItemAutomationPeer(System.Windows.Controls.MenuItem owner) : base (default(System.Windows.FrameworkElement)) { }
2913+
public MenuItemAutomationPeer(System.Windows.Controls.MenuItem owner) : base (default(System.Windows.Controls.ItemsControl)) { }
29132914
System.Windows.Automation.ExpandCollapseState System.Windows.Automation.Provider.IExpandCollapseProvider.ExpandCollapseState { get { throw null; } }
29142915
System.Windows.Automation.ToggleState System.Windows.Automation.Provider.IToggleProvider.ToggleState { get { throw null; } }
2916+
override protected System.Windows.Automation.Peers.ItemAutomationPeer CreateItemAutomationPeer(object item) { throw null; }
29152917
protected override string GetAccessKeyCore() { throw null; }
29162918
protected override System.Windows.Automation.Peers.AutomationControlType GetAutomationControlTypeCore() { throw null; }
29172919
protected override System.Collections.Generic.List<System.Windows.Automation.Peers.AutomationPeer> GetChildrenCore() { throw null; }

0 commit comments

Comments
 (0)