Skip to content

Commit

Permalink
(GH-3839) Move AutomationProperties bindings to the new HamburgerMenu…
Browse files Browse the repository at this point in the history
…ListBox
  • Loading branch information
punker76 committed Jun 17, 2020
1 parent 6ba2db2 commit bd2c15a
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 46 deletions.
Expand Up @@ -162,7 +162,7 @@
<!-- Items -->
<Controls:HamburgerMenu.ItemsSource>
<Controls:HamburgerMenuItemCollection>
<Controls:HamburgerMenuHeaderItem Label="Pictures" />
<Controls:HamburgerMenuHeaderItem Label="Pictures" AutomationProperties.Name="The Pictures Header" />
<Controls:HamburgerMenuGlyphItem Glyph="/Assets/Photos/BigFourSummerHeat.png" Label="Big four summer heat" />
<Controls:HamburgerMenuGlyphItem Glyph="/Assets/Photos/BisonBadlandsChillin.png" Label="Bison badlands Chillin" />
<Controls:HamburgerMenuSeparatorItem />
Expand Down
142 changes: 142 additions & 0 deletions src/MahApps.Metro/Controls/HamburgerMenu/HamburgerMenuListBox.cs
@@ -1,12 +1,15 @@
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Data;
using MahApps.Metro.Converters;

namespace MahApps.Metro.Controls
{
public class HamburgerMenuListBox : ListBox
{
private readonly BooleanToVisibilityConverter booleanToVisibilityConverter = new BooleanToVisibilityConverter();
private readonly HamburgerMenuItemAccessibleConverter hamburgerMenuItemAccessibleConverter = new HamburgerMenuItemAccessibleConverter();

static HamburgerMenuListBox()
{
Expand Down Expand Up @@ -40,6 +43,142 @@ protected override void PrepareContainerForItemOverride(DependencyObject element
Path = new PropertyPath(nameof(IHamburgerMenuItem.IsEnabled)),
FallbackValue = true
});

if (item is DependencyObject)
{
var helpTextPropertyMultiBinding = new MultiBinding
{
Converter = this.hamburgerMenuItemAccessibleConverter,
Bindings =
{
new Binding
{
Source = hamburgerMenuItem,
Path = new PropertyPath(nameof(IHamburgerMenuItem.ToolTip)),
Mode = BindingMode.OneWay,
FallbackValue = null
},
new Binding
{
Source = hamburgerMenuItem,
Path = new PropertyPath(AutomationProperties.HelpTextProperty),
Mode = BindingMode.OneWay,
FallbackValue = null
}
}
};
listBoxItem.SetBinding(AutomationProperties.HelpTextProperty, helpTextPropertyMultiBinding);

listBoxItem.SetBinding(AutomationProperties.LabeledByProperty,
new Binding
{
Source = hamburgerMenuItem,
Path = new PropertyPath(AutomationProperties.LabeledByProperty),
Mode = BindingMode.OneWay,
FallbackValue = null
});

var namePropertyMultiBinding = new MultiBinding
{
Converter = this.hamburgerMenuItemAccessibleConverter,
Bindings =
{
new Binding
{
Source = hamburgerMenuItem,
Path = new PropertyPath(nameof(IHamburgerMenuItem.Label)),
Mode = BindingMode.OneWay,
FallbackValue = null
},
new Binding
{
Source = hamburgerMenuItem,
Path = new PropertyPath(AutomationProperties.NameProperty),
Mode = BindingMode.OneWay,
FallbackValue = null
}
}
};
listBoxItem.SetBinding(AutomationProperties.NameProperty, namePropertyMultiBinding);
}
else
{
listBoxItem.SetBinding(AutomationProperties.HelpTextProperty,
new Binding
{
Source = hamburgerMenuItem,
Path = new PropertyPath(nameof(IHamburgerMenuItem.ToolTip)),
Mode = BindingMode.OneWay,
FallbackValue = null
});

listBoxItem.SetBinding(AutomationProperties.NameProperty,
new Binding
{
Source = hamburgerMenuItem,
Path = new PropertyPath(nameof(IHamburgerMenuItem.Label)),
Mode = BindingMode.OneWay,
FallbackValue = null
});
}
}

if (item is IHamburgerMenuHeaderItem hamburgerMenuHeaderItem)
{
if (item is DependencyObject)
{
listBoxItem.SetBinding(AutomationProperties.HelpTextProperty,
new Binding
{
Source = hamburgerMenuHeaderItem,
Path = new PropertyPath(AutomationProperties.HelpTextProperty),
Mode = BindingMode.OneWay,
FallbackValue = null
});

listBoxItem.SetBinding(AutomationProperties.LabeledByProperty,
new Binding
{
Source = hamburgerMenuHeaderItem,
Path = new PropertyPath(AutomationProperties.LabeledByProperty),
Mode = BindingMode.OneWay,
FallbackValue = null
});

var namePropertyMultiBinding = new MultiBinding
{
Converter = this.hamburgerMenuItemAccessibleConverter,
Bindings =
{
new Binding
{
Source = hamburgerMenuHeaderItem,
Path = new PropertyPath(nameof(IHamburgerMenuHeaderItem.Label)),
Mode = BindingMode.OneWay,
FallbackValue = null
},
new Binding
{
Source = hamburgerMenuHeaderItem,
Path = new PropertyPath(AutomationProperties.NameProperty),
Mode = BindingMode.OneWay,
FallbackValue = null
}
}
};
listBoxItem.SetBinding(AutomationProperties.NameProperty, namePropertyMultiBinding);
}
else
{
listBoxItem.SetBinding(AutomationProperties.NameProperty,
new Binding
{
Source = hamburgerMenuHeaderItem,
Path = new PropertyPath(nameof(IHamburgerMenuHeaderItem.Label)),
Mode = BindingMode.OneWay,
FallbackValue = null
});
}
}
}
}
Expand All @@ -52,6 +191,9 @@ protected override void ClearContainerForItemOverride(DependencyObject element,
{
BindingOperations.ClearBinding(listBoxItem, VisibilityProperty);
BindingOperations.ClearBinding(listBoxItem, IsEnabledProperty);
BindingOperations.ClearBinding(listBoxItem, AutomationProperties.HelpTextProperty);
BindingOperations.ClearBinding(listBoxItem, AutomationProperties.LabeledByProperty);
BindingOperations.ClearBinding(listBoxItem, AutomationProperties.NameProperty);
}
}
}
Expand Down
Expand Up @@ -2,6 +2,16 @@
{
public interface IHamburgerMenuItem
{
/// <summary>
/// Gets or sets a value that specifies label to display.
/// </summary>
string Label { get; set; }

/// <summary>
/// Gets or sets a value that specifies ToolTip to display.
/// </summary>
object ToolTip { get; set; }

/// <summary>
/// Gets or sets a value indicating whether this item is enabled in the user interface (UI).
/// </summary>
Expand Down
Expand Up @@ -8,9 +8,9 @@ namespace MahApps.Metro.Converters
{
[ValueConversion(typeof(object), typeof(object))]
[MarkupExtensionReturnType(typeof(HamburgerMenuItemAccessibleConverter))]
public class HamburgerMenuItemAccessibleConverter : MarkupMultiConverter
public class HamburgerMenuItemAccessibleConverter : IValueConverter, IMultiValueConverter
{
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is null)
{
Expand All @@ -20,12 +20,12 @@ public override object Convert(object value, Type targetType, object parameter,
return value;
}

public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
public 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)
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values is null)
{
Expand All @@ -41,7 +41,7 @@ public override object Convert(object[] values, Type targetType, object paramete
return this.Convert(values.ElementAtOrDefault(0), targetType, parameter, culture);
}

public override object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return targetTypes.Select(t => Binding.DoNothing).ToArray();
}
Expand Down
40 changes: 0 additions & 40 deletions src/MahApps.Metro/Themes/HamburgerMenuTemplate.xaml
@@ -1,7 +1,6 @@
<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 @@ -198,31 +197,6 @@
<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 @@ -325,20 +299,6 @@
<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

0 comments on commit bd2c15a

Please sign in to comment.