Skip to content

Commit d68b119

Browse files
Add TokenizingTextBox Workaround for IconSource FontSize Styling issue
Ref microsoft/microsoft-ui-xaml#2568 Also added WPF TryFindResource method to LogicalTree helpers - thanks for review/assist @rudyhuyn
1 parent dc0d231 commit d68b119

File tree

6 files changed

+48
-4
lines changed

6 files changed

+48
-4
lines changed

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/TabView/TabViewPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,5 @@ private void Tabs_TabDraggedOutside(object sender, TabDraggedOutsideEventArgs e)
6969
TabViewNotification.Show("Tore Tab '" + str + "' Outside of TabView.", 2000);
7070
}
7171
#pragma warning restore CS0618 // Type or member is obsolete
72-
}
72+
}
7373
}

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/TokenizingTextBox/TokenizingTextBoxXaml.bind

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<controls:TokenizingTextBox
1717
x:Name="TokenBox"
1818
PlaceholderText="Add Actions"
19-
QueryIcon="{ex:SymbolIconSource Glyph=Setting, FontSize=16}"
19+
QueryIcon="{ex:SymbolIconSource Glyph=Setting}"
2020
MaxHeight="104"
2121
HorizontalAlignment="Stretch"
2222
TextMemberPath="Text"
@@ -52,7 +52,7 @@
5252
PlaceholderText="Select Names"
5353
MaxHeight="104"
5454
HorizontalAlignment="Stretch"
55-
QueryIcon="{ex:SymbolIconSource Glyph=Find, FontSize=16}"
55+
QueryIcon="{ex:SymbolIconSource Glyph=Find}"
5656
TextMemberPath="Text"
5757
TokenDelimiter=","
5858
IsItemClickEnabled="True">

Microsoft.Toolkit.Uwp.UI.Controls/TokenizingTextBox/TokenizingTextBox.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<Thickness x:Key="TokenizingTextBoxPresenterMargin">0,0,6,0</Thickness>
1313
<x:Double x:Key="TokenizingTextBoxTokenSpacing">2</x:Double>
1414
<Thickness x:Key="TextControlBorderThemeThicknessFocused">2</Thickness>
15+
<x:Double x:Key="TokenizingTextBoxIconFontSize">16</x:Double>
1516

1617
<controls:TokenizingTextBoxStyleSelector x:Key="TokenizingTextBoxStyleSelector"
1718
TextStyle="{StaticResource TokenizingTextBoxItemTextStyle}"

Microsoft.Toolkit.Uwp.UI.Controls/TokenizingTextBox/TokenizingTextBoxItem.AutoSuggestBox.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ private void OnApplyTemplateAutoSuggestBox(AutoSuggestBox auto)
101101
// Setup a binding to the QueryIcon of the Parent if we're the last box.
102102
if (Content is PretokenStringContainer str && str.IsLast)
103103
{
104+
// Workaround for https://github.com/microsoft/microsoft-ui-xaml/issues/2568
105+
if (Owner.QueryIcon is FontIconSource fis &&
106+
fis.ReadLocalValue(FontIconSource.FontSizeProperty) == DependencyProperty.UnsetValue)
107+
{
108+
// This can be expensive, could we optimize?
109+
// Also, this is changing the FontSize on the IconSource (which could be shared?)
110+
fis.FontSize = Owner.TryFindResource("TokenizingTextBoxIconFontSize") as double? ?? 16;
111+
}
112+
104113
var iconBinding = new Binding()
105114
{
106115
Source = Owner,

Microsoft.Toolkit.Uwp.UI.Controls/TokenizingTextBox/TokenizingTextBoxItem.AutoSuggestBox.xaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
Background="{ThemeResource TextControlButtonBackground}"
1616
BorderBrush="{ThemeResource TextControlButtonBorderBrush}"
1717
BorderThickness="{TemplateBinding BorderThickness}">
18+
<!-- FontSize is ignored here, see https://github.com/microsoft/microsoft-ui-xaml/issues/2568 -->
19+
<!-- Set in code-behind link:TokenizingTextBoxItem.AutoSuggestBox.cs#L104 -->
1820
<TextBlock x:Name="GlyphElement"
1921
HorizontalAlignment="Center"
2022
VerticalAlignment="Center"
2123
AutomationProperties.AccessibilityView="Raw"
2224
FontFamily="{ThemeResource SymbolThemeFontFamily}"
23-
FontSize="{ThemeResource AutoSuggestBoxIconFontSize}"
25+
FontSize="{ThemeResource TokenizingTextBoxIconFontSize}"
2426
FontStyle="Normal"
2527
Foreground="{ThemeResource TextControlButtonForeground}"
2628
Text="&#xE10A;" />

Microsoft.Toolkit.Uwp.UI/Extensions/Tree/LogicalTree.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,5 +286,37 @@ private static string ContentPropertySearch(Type type)
286286

287287
return ContentPropertySearch(type.GetTypeInfo().BaseType);
288288
}
289+
290+
/// <summary>
291+
/// Provides a WPF compatible version of TryFindResource to provide a static resource lookup.
292+
/// 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.
293+
/// If none of the elements contain the resource, the Application's resources are then searched.
294+
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.tryfindresource"/>
295+
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/desktop-wpf/fundamentals/xaml-resources-define#static-resource-lookup-behavior"/>
296+
/// </summary>
297+
/// <param name="start"><see cref="FrameworkElement"/> to start searching for Resource.</param>
298+
/// <param name="resourceKey">Key to search for.</param>
299+
/// <returns>Requested resource or null.</returns>
300+
public static object TryFindResource(this FrameworkElement start, object resourceKey)
301+
{
302+
object value = null;
303+
var current = start;
304+
305+
// Look in our dictionary and then walk-up parents
306+
while (current != null)
307+
{
308+
if (current.Resources?.TryGetValue(resourceKey, out value) == true)
309+
{
310+
return value;
311+
}
312+
313+
current = current.Parent as FrameworkElement;
314+
}
315+
316+
// Finally try application resources.
317+
Application.Current?.Resources?.TryGetValue(resourceKey, out value);
318+
319+
return value;
320+
}
289321
}
290322
}

0 commit comments

Comments
 (0)