Skip to content

Commit

Permalink
(GH-3839) Fix BindingExpression errors and use empty string for Fallb…
Browse files Browse the repository at this point in the history
…ackValue
  • Loading branch information
punker76 committed Jun 18, 2020
1 parent bd2c15a commit 363d94a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
33 changes: 19 additions & 14 deletions src/MahApps.Metro/Controls/HamburgerMenu/HamburgerMenuListBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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 @@ -48,22 +47,24 @@ protected override void PrepareContainerForItemOverride(DependencyObject element
{
var helpTextPropertyMultiBinding = new MultiBinding
{
Converter = this.hamburgerMenuItemAccessibleConverter,
Converter = HamburgerMenuItemAccessibleConverter.Default,
Mode = BindingMode.OneWay,
FallbackValue = string.Empty,
Bindings =
{
new Binding
{
Source = hamburgerMenuItem,
Path = new PropertyPath(nameof(IHamburgerMenuItem.ToolTip)),
Mode = BindingMode.OneWay,
FallbackValue = null
FallbackValue = string.Empty
},
new Binding
{
Source = hamburgerMenuItem,
Path = new PropertyPath(AutomationProperties.HelpTextProperty),
Mode = BindingMode.OneWay,
FallbackValue = null
FallbackValue = string.Empty
}
}
};
Expand All @@ -80,22 +81,24 @@ protected override void PrepareContainerForItemOverride(DependencyObject element

var namePropertyMultiBinding = new MultiBinding
{
Converter = this.hamburgerMenuItemAccessibleConverter,
Converter = HamburgerMenuItemAccessibleConverter.Default,
Mode = BindingMode.OneWay,
FallbackValue = string.Empty,
Bindings =
{
new Binding
{
Source = hamburgerMenuItem,
Path = new PropertyPath(nameof(IHamburgerMenuItem.Label)),
Mode = BindingMode.OneWay,
FallbackValue = null
FallbackValue = string.Empty
},
new Binding
{
Source = hamburgerMenuItem,
Path = new PropertyPath(AutomationProperties.NameProperty),
Mode = BindingMode.OneWay,
FallbackValue = null
FallbackValue = string.Empty
}
}
};
Expand All @@ -109,7 +112,7 @@ protected override void PrepareContainerForItemOverride(DependencyObject element
Source = hamburgerMenuItem,
Path = new PropertyPath(nameof(IHamburgerMenuItem.ToolTip)),
Mode = BindingMode.OneWay,
FallbackValue = null
FallbackValue = string.Empty
});

listBoxItem.SetBinding(AutomationProperties.NameProperty,
Expand All @@ -118,7 +121,7 @@ protected override void PrepareContainerForItemOverride(DependencyObject element
Source = hamburgerMenuItem,
Path = new PropertyPath(nameof(IHamburgerMenuItem.Label)),
Mode = BindingMode.OneWay,
FallbackValue = null
FallbackValue = string.Empty
});
}
}
Expand All @@ -133,7 +136,7 @@ protected override void PrepareContainerForItemOverride(DependencyObject element
Source = hamburgerMenuHeaderItem,
Path = new PropertyPath(AutomationProperties.HelpTextProperty),
Mode = BindingMode.OneWay,
FallbackValue = null
FallbackValue = string.Empty
});

listBoxItem.SetBinding(AutomationProperties.LabeledByProperty,
Expand All @@ -147,22 +150,24 @@ protected override void PrepareContainerForItemOverride(DependencyObject element

var namePropertyMultiBinding = new MultiBinding
{
Converter = this.hamburgerMenuItemAccessibleConverter,
Converter = HamburgerMenuItemAccessibleConverter.Default,
Mode = BindingMode.OneWay,
FallbackValue = string.Empty,
Bindings =
{
new Binding
{
Source = hamburgerMenuHeaderItem,
Path = new PropertyPath(nameof(IHamburgerMenuHeaderItem.Label)),
Mode = BindingMode.OneWay,
FallbackValue = null
FallbackValue = string.Empty
},
new Binding
{
Source = hamburgerMenuHeaderItem,
Path = new PropertyPath(AutomationProperties.NameProperty),
Mode = BindingMode.OneWay,
FallbackValue = null
FallbackValue = string.Empty
}
}
};
Expand All @@ -176,7 +181,7 @@ protected override void PrepareContainerForItemOverride(DependencyObject element
Source = hamburgerMenuHeaderItem,
Path = new PropertyPath(nameof(IHamburgerMenuHeaderItem.Label)),
Mode = BindingMode.OneWay,
FallbackValue = null
FallbackValue = string.Empty
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,14 @@
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 : IValueConverter, IMultiValueConverter
internal sealed class HamburgerMenuItemAccessibleConverter : IMultiValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is null)
{
return Binding.DoNothing;
}

return value;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Binding.DoNothing;
}
/// <summary> Gets the default instance </summary>
internal static HamburgerMenuItemAccessibleConverter Default { get; } = new HamburgerMenuItemAccessibleConverter();

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
Expand All @@ -38,7 +24,13 @@ public object Convert(object[] values, Type targetType, object parameter, Cultur
return automationPropertiesValue;
}

return this.Convert(values.ElementAtOrDefault(0), targetType, parameter, culture);
var menuItemValue = values.ElementAtOrDefault(0) as string;
if (!string.IsNullOrEmpty(menuItemValue))
{
return menuItemValue;
}

return Binding.DoNothing;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
Expand Down

0 comments on commit 363d94a

Please sign in to comment.