Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara authored Nov 22, 2023
1 parent e5c8538 commit b3e8c2e
Show file tree
Hide file tree
Showing 9 changed files with 492 additions and 3 deletions.
69 changes: 69 additions & 0 deletions ModernWpf.Controls/Common/SharedHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,75 @@ public static void RaiseAutomationPropertyChangedEvent(UIElement element, object
}
}

public static IconElement MakeIconElementFrom(IconSource iconSource)
{
if (iconSource is FontIconSource fontIconSource)
{
FontIcon fontIcon = new();

fontIcon.Glyph = fontIconSource.Glyph;
fontIcon.FontSize = fontIconSource.FontSize;
if (fontIconSource.Foreground is { } newForeground)
{
fontIcon.Foreground = newForeground;
}

if (fontIconSource.FontFamily != null)
{
fontIcon.FontFamily = fontIconSource.FontFamily;
}

fontIcon.FontWeight = fontIconSource.FontWeight;
fontIcon.FontStyle = fontIconSource.FontStyle;
//fontIcon.IsTextScaleFactorEnabled(fontIconSource.IsTextScaleFactorEnabled());
//fontIcon.MirroredWhenRightToLeft(fontIconSource.MirroredWhenRightToLeft());

return fontIcon;
}
else if (iconSource is SymbolIconSource symbolIconSource)
{
SymbolIcon symbolIcon = new();
symbolIcon.Symbol = symbolIconSource.Symbol;
if (symbolIconSource.Foreground is { } newForeground)
{
symbolIcon.Foreground = newForeground;
}
return symbolIcon;
}
else if (iconSource is BitmapIconSource bitmapIconSource)
{
BitmapIcon bitmapIcon = new();

if (bitmapIconSource.UriSource != null)
{
bitmapIcon.UriSource = bitmapIconSource.UriSource;
}

bitmapIcon.ShowAsMonochrome = bitmapIconSource.ShowAsMonochrome;
if (bitmapIconSource.Foreground is { } newForeground)
{
bitmapIcon.Foreground = newForeground;
}
return bitmapIcon;
}
else if (iconSource is PathIconSource pathIconSource)
{
PathIcon pathIcon = new();

if (pathIconSource.Data != null)
{
pathIcon.Data = pathIconSource.Data;
}
if (pathIconSource.Foreground is { } newForeground)
{
pathIcon.Foreground = newForeground;
}
return pathIcon;
}

return null;
}

public static BindingExpressionBase SetBinding(
this FrameworkElement element,
DependencyProperty dp,
Expand Down
1 change: 0 additions & 1 deletion ModernWpf.Controls/DropDownButton/DropDownButton.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
Grid.Column="1"
FontFamily="{DynamicResource SymbolThemeFontFamily}"
FontSize="8"
Foreground="{DynamicResource ButtonForegroundPressed}"
Data="{StaticResource ChevronDownSmall}"
VerticalAlignment="Center"
Padding="2,4,2,0"
Expand Down
14 changes: 14 additions & 0 deletions ModernWpf/IconSource/BitmapIconSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,19 @@ protected override IconElement CreateIconElementCore()
}
return bitmapIcon;
}

protected override DependencyProperty GetIconElementPropertyCore(DependencyProperty sourceProperty)
{
if (sourceProperty == ShowAsMonochromeProperty)
{
return BitmapIcon.ShowAsMonochromeProperty;
}
else if (sourceProperty == UriSourceProperty)
{
return BitmapIcon.UriSourceProperty;
}

return base.GetIconElementPropertyCore(sourceProperty);
}
}
}
34 changes: 34 additions & 0 deletions ModernWpf/IconSource/FontIconSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,39 @@ protected override IconElement CreateIconElementCore()

return fontIcon;
}

protected override DependencyProperty GetIconElementPropertyCore(DependencyProperty sourceProperty)
{
if (sourceProperty == FontFamilyProperty)
{
return FontIcon.FontFamilyProperty;
}
else if (sourceProperty == FontSizeProperty)
{
return FontIcon.FontSizeProperty;
}
else if (sourceProperty == FontStyleProperty)
{
return FontIcon.FontStyleProperty;
}
else if (sourceProperty == FontWeightProperty)
{
return FontIcon.FontWeightProperty;
}
else if (sourceProperty == GlyphProperty)
{
return FontIcon.GlyphProperty;
}
//else if (sourceProperty == IsTextScaleFactorEnabledProperty)
//{
// return FontIcon.IsTextScaleFactorEnabledProperty;
//}
//else if (sourceProperty == MirroredWhenRightToLeftProperty)
//{
// return FontIcon.MirroredWhenRightToLeftProperty;
//}

return base.GetIconElementPropertyCore(sourceProperty);
}
}
}
37 changes: 36 additions & 1 deletion ModernWpf/IconSource/IconSource.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;

Expand Down Expand Up @@ -35,9 +37,42 @@ public Brush Foreground

public IconElement CreateIconElement()
{
return CreateIconElementCore();
var element = CreateIconElementCore();
m_createdIconElements.Add(new WeakReference<IconElement>(element));
return element;
}

protected abstract IconElement CreateIconElementCore();

protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs args)
{
base.OnPropertyChanged(args);

var iconProp = GetIconElementPropertyCore(args.Property);
if (iconProp != null)
{
m_createdIconElements.RemoveAll(weakElement =>
{
if (weakElement.TryGetTarget(out var element))
{
element.SetValue(iconProp, args.NewValue);
return false;
}
return true;
});
}
}

protected virtual DependencyProperty GetIconElementPropertyCore(DependencyProperty sourceProperty)
{
if (sourceProperty == ForegroundProperty)
{
return IconElement.ForegroundProperty;
}

return null;
}

private readonly List<WeakReference<IconElement>> m_createdIconElements = new();
}
}
10 changes: 10 additions & 0 deletions ModernWpf/IconSource/PathIconSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,15 @@ protected override IconElement CreateIconElementCore()
}
return pathIcon;
}

protected override DependencyProperty GetIconElementPropertyCore(DependencyProperty sourceProperty)
{
if (sourceProperty == DataProperty)
{
return PathIcon.DataProperty;
}

return base.GetIconElementPropertyCore(sourceProperty);
}
}
}
10 changes: 10 additions & 0 deletions ModernWpf/IconSource/SymbolIconSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,15 @@ protected override IconElement CreateIconElementCore()
}
return symbolIcon;
}

protected override DependencyProperty GetIconElementPropertyCore(DependencyProperty sourceProperty)
{
if (sourceProperty == SymbolProperty)
{
return SymbolIcon.SymbolProperty;
}

return base.GetIconElementPropertyCore(sourceProperty);
}
}
}
3 changes: 2 additions & 1 deletion ModernWpf/Styles/CheckBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<sys:Double x:Key="CheckBoxBorderThickness">1</sys:Double>
<sys:Double x:Key="CheckBoxSize">20</sys:Double>
<sys:Double x:Key="CheckBoxGlyphSize">16</sys:Double>
<sys:Double x:Key="CheckBoxGlyphSize">12</sys:Double>
<sys:Double x:Key="CheckBoxHeight">32</sys:Double>
<sys:Double x:Key="CheckBoxMinWidth">120</sys:Double>
<Thickness x:Key="CheckBoxPadding">8,5,0,0</Thickness>
Expand Down Expand Up @@ -76,6 +76,7 @@
Data="{StaticResource CheckMark}"
FontSize="{StaticResource CheckBoxGlyphSize}"
Foreground="{DynamicResource CheckBoxCheckGlyphForegroundUnchecked}"
Margin="4"
Opacity="0" />
</Grid>
<local:ContentPresenterEx
Expand Down
Loading

0 comments on commit b3e8c2e

Please sign in to comment.