Skip to content

Commit

Permalink
Add TokenizingTextBox Workaround for IconSource FontSize Styling issue
Browse files Browse the repository at this point in the history
Ref microsoft/microsoft-ui-xaml#2568
Also added WPF TryFindResource method to LogicalTree helpers - thanks for review/assist @rudyhuyn
  • Loading branch information
michael-hawker committed Jun 3, 2020
1 parent dc0d231 commit d68b119
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ private void Tabs_TabDraggedOutside(object sender, TabDraggedOutsideEventArgs e)
TabViewNotification.Show("Tore Tab '" + str + "' Outside of TabView.", 2000);
}
#pragma warning restore CS0618 // Type or member is obsolete
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<controls:TokenizingTextBox
x:Name="TokenBox"
PlaceholderText="Add Actions"
QueryIcon="{ex:SymbolIconSource Glyph=Setting, FontSize=16}"
QueryIcon="{ex:SymbolIconSource Glyph=Setting}"
MaxHeight="104"
HorizontalAlignment="Stretch"
TextMemberPath="Text"
Expand Down Expand Up @@ -52,7 +52,7 @@
PlaceholderText="Select Names"
MaxHeight="104"
HorizontalAlignment="Stretch"
QueryIcon="{ex:SymbolIconSource Glyph=Find, FontSize=16}"
QueryIcon="{ex:SymbolIconSource Glyph=Find}"
TextMemberPath="Text"
TokenDelimiter=","
IsItemClickEnabled="True">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<Thickness x:Key="TokenizingTextBoxPresenterMargin">0,0,6,0</Thickness>
<x:Double x:Key="TokenizingTextBoxTokenSpacing">2</x:Double>
<Thickness x:Key="TextControlBorderThemeThicknessFocused">2</Thickness>
<x:Double x:Key="TokenizingTextBoxIconFontSize">16</x:Double>

<controls:TokenizingTextBoxStyleSelector x:Key="TokenizingTextBoxStyleSelector"
TextStyle="{StaticResource TokenizingTextBoxItemTextStyle}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ private void OnApplyTemplateAutoSuggestBox(AutoSuggestBox auto)
// Setup a binding to the QueryIcon of the Parent if we're the last box.
if (Content is PretokenStringContainer str && str.IsLast)
{
// Workaround for https://github.com/microsoft/microsoft-ui-xaml/issues/2568
if (Owner.QueryIcon is FontIconSource fis &&
fis.ReadLocalValue(FontIconSource.FontSizeProperty) == DependencyProperty.UnsetValue)
{
// This can be expensive, could we optimize?
// Also, this is changing the FontSize on the IconSource (which could be shared?)
fis.FontSize = Owner.TryFindResource("TokenizingTextBoxIconFontSize") as double? ?? 16;
}

var iconBinding = new Binding()
{
Source = Owner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
Background="{ThemeResource TextControlButtonBackground}"
BorderBrush="{ThemeResource TextControlButtonBorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<!-- FontSize is ignored here, see https://github.com/microsoft/microsoft-ui-xaml/issues/2568 -->
<!-- Set in code-behind link:TokenizingTextBoxItem.AutoSuggestBox.cs#L104 -->
<TextBlock x:Name="GlyphElement"
HorizontalAlignment="Center"
VerticalAlignment="Center"
AutomationProperties.AccessibilityView="Raw"
FontFamily="{ThemeResource SymbolThemeFontFamily}"
FontSize="{ThemeResource AutoSuggestBoxIconFontSize}"
FontSize="{ThemeResource TokenizingTextBoxIconFontSize}"
FontStyle="Normal"
Foreground="{ThemeResource TextControlButtonForeground}"
Text="&#xE10A;" />
Expand Down
32 changes: 32 additions & 0 deletions Microsoft.Toolkit.Uwp.UI/Extensions/Tree/LogicalTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,5 +286,37 @@ private static string ContentPropertySearch(Type type)

return ContentPropertySearch(type.GetTypeInfo().BaseType);
}

/// <summary>
/// Provides a WPF compatible version of TryFindResource to provide a static resource lookup.
/// If the key is not found in the current element's resources, the logical tree is then searched element-by-element to look for the resource in each element's resources.
/// If none of the elements contain the resource, the Application's resources are then searched.
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.tryfindresource"/>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/desktop-wpf/fundamentals/xaml-resources-define#static-resource-lookup-behavior"/>
/// </summary>
/// <param name="start"><see cref="FrameworkElement"/> to start searching for Resource.</param>
/// <param name="resourceKey">Key to search for.</param>
/// <returns>Requested resource or null.</returns>
public static object TryFindResource(this FrameworkElement start, object resourceKey)
{
object value = null;
var current = start;

// Look in our dictionary and then walk-up parents
while (current != null)
{
if (current.Resources?.TryGetValue(resourceKey, out value) == true)
{
return value;
}

current = current.Parent as FrameworkElement;
}

// Finally try application resources.
Application.Current?.Resources?.TryGetValue(resourceKey, out value);

return value;
}
}
}

0 comments on commit d68b119

Please sign in to comment.