Skip to content

Commit

Permalink
(GH-3839) Allow setting AutomationProperties for HamburgerMenuItem
Browse files Browse the repository at this point in the history
Allow setting the AutomationProperties for HamburgerMenuItem

- AutomationProperties.HelpText
- AutomationProperties.LabeledBy
- AutomationProperties.Name

If Name or HelpText are empty the Label and ToolTip properties will be used.
  • Loading branch information
punker76 committed Jun 17, 2020
1 parent 0117888 commit 6ba2db2
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
Expand Up @@ -205,7 +205,10 @@
<!-- Items -->
<Controls:HamburgerMenu.ItemsSource>
<Controls:HamburgerMenuItemCollection>
<Controls:HamburgerMenuGlyphItem Glyph="/Assets/Photos/BigFourSummerHeat.png" Label="Big four summer heat" />
<Controls:HamburgerMenuGlyphItem AutomationProperties.HelpText="First Item shows BigFourSummerHeat"
AutomationProperties.Name="First Item"
Glyph="/Assets/Photos/BigFourSummerHeat.png"
Label="Big four summer heat" />
<Controls:HamburgerMenuGlyphItem Glyph="/Assets/Photos/BisonBadlandsChillin.png" Label="Bison badlands Chillin" />
<Controls:HamburgerMenuGlyphItem Glyph="/Assets/Photos/GiantSlabInOregon.png" Label="Giant slab in Oregon" />
<Controls:HamburgerMenuGlyphItem Glyph="/Assets/Photos/LakeAnnMushroom.png" Label="Lake Ann Mushroom" />
Expand Down
@@ -0,0 +1,49 @@
using System;
using System.Globalization;
using System.Linq;
using System.Windows.Data;
using System.Windows.Markup;

namespace MahApps.Metro.Converters
{
[ValueConversion(typeof(object), typeof(object))]
[MarkupExtensionReturnType(typeof(HamburgerMenuItemAccessibleConverter))]
public class HamburgerMenuItemAccessibleConverter : MarkupMultiConverter
{
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is null)
{
return Binding.DoNothing;
}

return value;
}

public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Binding.DoNothing;
}

public override object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values is null)
{
return Binding.DoNothing;
}

var automationPropertiesValue = values.ElementAtOrDefault(1) as string;
if (!string.IsNullOrEmpty(automationPropertiesValue))
{
return automationPropertiesValue;
}

return this.Convert(values.ElementAtOrDefault(0), targetType, parameter, culture);
}

public override object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return targetTypes.Select(t => Binding.DoNothing).ToArray();
}
}
}
41 changes: 41 additions & 0 deletions src/MahApps.Metro/Themes/HamburgerMenuTemplate.xaml
@@ -1,6 +1,7 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls"
xmlns:converters="clr-namespace:MahApps.Metro.Converters"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:options="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
xmlns:system="clr-namespace:System;assembly=mscorlib"
Expand Down Expand Up @@ -197,6 +198,31 @@
<Style x:Key="MahApps.Styles.ListBoxItem.HamburgerMenuItem"
BasedOn="{StaticResource MahApps.Styles.ListBoxItem.HamburgerBase}"
TargetType="{x:Type ListBoxItem}">
<Setter Property="AutomationProperties.HelpText">
<Setter.Value>
<MultiBinding Converter="{converters:HamburgerMenuItemAccessibleConverter}">
<Binding FallbackValue="{x:Null}"
Mode="OneWay"
Path="ToolTip" />
<Binding FallbackValue="{x:Null}"
Mode="OneWay"
Path="(AutomationProperties.HelpText)" />
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="AutomationProperties.LabeledBy" Value="{Binding Path=(AutomationProperties.LabeledBy), Mode=OneWay, FallbackValue={x:Null}}" />
<Setter Property="AutomationProperties.Name">
<Setter.Value>
<MultiBinding Converter="{converters:HamburgerMenuItemAccessibleConverter}">
<Binding FallbackValue="{x:Null}"
Mode="OneWay"
Path="Label" />
<Binding FallbackValue="{x:Null}"
Mode="OneWay"
Path="(AutomationProperties.Name)" />
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="Controls:ItemHelper.SelectedBackgroundBrush" Value="{DynamicResource MahApps.Brushes.Accent}" />
<Setter Property="FocusVisualStyle" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:HamburgerMenu}}, Path=ItemFocusVisualStyle, Mode=OneWay, FallbackValue={x:Null}}" />
<Setter Property="IsTabStop" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:HamburgerMenu}}, Path=IsTabStop, Mode=OneWay, FallbackValue=True}" />
Expand Down Expand Up @@ -299,6 +325,20 @@
<Style x:Key="MahApps.Styles.ListBoxItem.HamburgerMenuHeader"
BasedOn="{StaticResource MahApps.Styles.ListBoxItem.HamburgerBase}"
TargetType="{x:Type ListBoxItem}">
<Setter Property="AutomationProperties.HelpText" Value="{Binding Path=(AutomationProperties.HelpText), Mode=OneWay, FallbackValue={x:Null}}" />
<Setter Property="AutomationProperties.LabeledBy" Value="{Binding Path=(AutomationProperties.LabeledBy), Mode=OneWay, FallbackValue={x:Null}}" />
<Setter Property="AutomationProperties.Name">
<Setter.Value>
<MultiBinding Converter="{converters:HamburgerMenuItemAccessibleConverter}">
<Binding FallbackValue="{x:Null}"
Mode="OneWay"
Path="Label" />
<Binding FallbackValue="{x:Null}"
Mode="OneWay"
Path="(AutomationProperties.Name)" />
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Focusable" Value="False" />
<Setter Property="HorizontalContentAlignment" Value="Left" />
Expand Down Expand Up @@ -425,6 +465,7 @@
Height="{TemplateBinding HamburgerHeight}"
Padding="{TemplateBinding HamburgerMargin}"
AutomationProperties.Name="Main button"
AutomationProperties.AutomationId="HamburgerButton"
ContentTemplate="{TemplateBinding HamburgerButtonTemplate}"
DockPanel.Dock="Left"
Foreground="{TemplateBinding PaneForeground}"
Expand Down

0 comments on commit 6ba2db2

Please sign in to comment.