Skip to content

Commit

Permalink
Changed ContentControl to ContentPresenter
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanDmitriev1 committed Mar 16, 2023
1 parent ad09c69 commit 2b54a5b
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 129 deletions.
29 changes: 7 additions & 22 deletions src/Wpf.Ui/Controls/IconElements/FontIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Drawing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using FontStyle = System.Windows.FontStyle;
using SystemFonts = System.Windows.SystemFonts;
Expand Down Expand Up @@ -40,7 +39,8 @@ public class FontIcon : IconElement
/// Property for <see cref="FontSize"/>.
/// </summary>
public static readonly DependencyProperty FontSizeProperty =
TextElement.FontSizeProperty.AddOwner(typeof(FontIcon),
TextElement.FontSizeProperty.AddOwner(
typeof(FontIcon),
new FrameworkPropertyMetadata(SystemFonts.MessageFontSize, FrameworkPropertyMetadataOptions.Inherits,
OnFontSizeChanged));

Expand Down Expand Up @@ -129,6 +129,11 @@ public string Glyph

protected override UIElement InitializeChildren()
{
if (VisualParent is not null)
{
FontSize = TextElement.GetFontSize(VisualParent);
}

if (FontSize.Equals(SystemFonts.MessageFontSize))
{
SetResourceReference(FontSizeProperty, "DefaultIconFontSize");
Expand All @@ -154,26 +159,6 @@ protected override UIElement InitializeChildren()
return TextBlock;
}

protected override void OnShouldInheritForegroundFromVisualParentChanged()
{
if (TextBlock is null)
return;

if (ShouldInheritForegroundFromVisualParent)
{
TextBlock.SetBinding(TextBlock.ForegroundProperty,
new Binding
{
Path = new PropertyPath(TextElement.ForegroundProperty),
Source = VisualParent,
});
}
else
{
TextBlock.ClearValue(TextBlock.ForegroundProperty);
}
}

#region Static methods

private static void OnFontFamilyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
Expand Down
43 changes: 2 additions & 41 deletions src/Wpf.Ui/Controls/IconElements/IconElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,56 +43,17 @@ public Brush Foreground
set => SetValue(ForegroundProperty, value);
}

protected bool ShouldInheritForegroundFromVisualParent
{
get => _shouldInheritForegroundFromVisualParent;
private set
{
if (_shouldInheritForegroundFromVisualParent == value)
return;

_shouldInheritForegroundFromVisualParent = value;
OnShouldInheritForegroundFromVisualParentChanged();
}
}

protected override int VisualChildrenCount => 1;

private bool _shouldInheritForegroundFromVisualParent;
private Grid? _layoutRoot;
private bool _isForegroundDefaultOrInherited = true;

#region Protected methods

protected abstract void OnShouldInheritForegroundFromVisualParentChanged();

protected abstract UIElement InitializeChildren();

#endregion

private void OnForegroundPropertyChanged(DependencyPropertyChangedEventArgs e)
{
var baseValueSource = DependencyPropertyHelper.GetValueSource(this, e.Property).BaseValueSource;
_isForegroundDefaultOrInherited = baseValueSource <= BaseValueSource.Inherited;
UpdateShouldInheritForegroundFromVisualParent();
}

private void UpdateShouldInheritForegroundFromVisualParent()
{
ShouldInheritForegroundFromVisualParent =
_isForegroundDefaultOrInherited &&
Parent != null &&
VisualParent != null &&
Parent != VisualParent;
}

protected override void OnVisualParentChanged(DependencyObject oldParent)
{
base.OnVisualParentChanged(oldParent);

UpdateShouldInheritForegroundFromVisualParent();
}
protected virtual void OnForegroundPropertyChanged(DependencyPropertyChangedEventArgs args) { }

#endregion

#region Layout methods

Expand Down
5 changes: 0 additions & 5 deletions src/Wpf.Ui/Controls/IconElements/IconSourceElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ public IconSource? IconSource
set => SetValue(IconSourceProperty, value);
}

protected override void OnShouldInheritForegroundFromVisualParentChanged()
{

}

protected override UIElement InitializeChildren()
{
throw new InvalidOperationException($"Use {nameof(IconSourceElementConverter)} class.");
Expand Down
5 changes: 0 additions & 5 deletions src/Wpf.Ui/Controls/IconElements/ImageIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ protected override UIElement InitializeChildren()
return Image;
}

protected override void OnShouldInheritForegroundFromVisualParentChanged()
{

}

private static void OnSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var self = (ImageIcon)d;
Expand Down
6 changes: 3 additions & 3 deletions src/Wpf.Ui/Controls/IconElements/SymbolIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);

OnFilledChanged();
SetFontReference();
}

private void OnGlyphChanged()
Expand All @@ -73,15 +73,15 @@ private void OnGlyphChanged()
Glyph = Symbol.GetString();
}

private void OnFilledChanged()
private void SetFontReference()
{
SetResourceReference(FontFamilyProperty, Filled ? "FluentSystemIconsFilled" : "FluentSystemIcons");
}

private static void OnFilledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var self = (SymbolIcon)d;
self.OnFilledChanged();
self.SetFontReference();
self.OnGlyphChanged();
}
}
4 changes: 2 additions & 2 deletions src/Wpf.Ui/Controls/TitleBarControl/TitleBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public event TypedEventHandler<TitleBar, RoutedEventArgs> HelpClicked

private System.Windows.Window _currentWindow = null!;
private System.Windows.Controls.Grid _mainGrid = null!;
private System.Windows.Controls.ContentControl _icon = null!;
private System.Windows.Controls.ContentPresenter _icon = null!;
private readonly TitleBarButton[] _buttons = new TitleBarButton[4];

/// <summary>
Expand Down Expand Up @@ -435,7 +435,7 @@ public override void OnApplyTemplate()
base.OnApplyTemplate();

_mainGrid = GetTemplateChild<System.Windows.Controls.Grid>(ElementMainGrid);
_icon = GetTemplateChild<System.Windows.Controls.ContentControl>(ElementIcon);
_icon = GetTemplateChild<System.Windows.Controls.ContentPresenter>(ElementIcon);

var helpButton = GetTemplateChild<TitleBarButton>(ElementHelpButton);
var minimizeButton = GetTemplateChild<TitleBarButton>(ElementMinimizeButton);
Expand Down
11 changes: 11 additions & 0 deletions src/Wpf.Ui/Styles/Controls/AutoSuggestBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:autoSuggestBoxControl="clr-namespace:Wpf.Ui.Controls.AutoSuggestBoxControl"
xmlns:controls="clr-namespace:Wpf.Ui.Controls"
xmlns:iconElements="clr-namespace:Wpf.Ui.Controls.IconElements"
xmlns:iconSources="clr-namespace:Wpf.Ui.Controls.IconSources"
xmlns:system="clr-namespace:System;assembly=mscorlib">

<Thickness x:Key="AutoSuggestBoxBorderThemeThickness">1,1,1,0</Thickness>
Expand Down Expand Up @@ -98,6 +100,15 @@
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="ItemContainerStyle" Value="{StaticResource DefaultAutoSuggestBoxItemContainerStyle}" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Icon">
<Setter.Value>
<iconElements:IconSourceElement>
<iconElements:IconSourceElement.IconSource>
<iconSources:SymbolIconSource Symbol="Search24" />
</iconElements:IconSourceElement.IconSource>
</iconElements:IconSourceElement>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type autoSuggestBoxControl:AutoSuggestBox}">
Expand Down
6 changes: 3 additions & 3 deletions src/Wpf.Ui/Styles/Controls/Button.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,14 @@
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<ContentControl
<ContentPresenter
x:Name="ControlIcon"
Grid.Column="0"
Margin="{StaticResource ButtonIconMargin}"
VerticalAlignment="Center"
Content="{TemplateBinding Icon}"
Focusable="False"
Foreground="{TemplateBinding Foreground}" />
TextElement.Foreground="{TemplateBinding Foreground}" />

<ContentPresenter
x:Name="ContentPresenter"
Expand All @@ -229,7 +229,7 @@
<Setter TargetName="ContentBorder" Property="Background" Value="{Binding PressedBackground, RelativeSource={RelativeSource TemplatedParent}}" />
<Setter TargetName="ContentBorder" Property="BorderBrush" Value="{Binding PressedBorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />
<Setter TargetName="ContentPresenter" Property="TextElement.Foreground" Value="{Binding PressedForeground, RelativeSource={RelativeSource TemplatedParent}}" />
<Setter TargetName="ControlIcon" Property="Foreground" Value="{Binding PressedForeground, RelativeSource={RelativeSource TemplatedParent}}" />
<Setter TargetName="ControlIcon" Property="TextElement.Foreground" Value="{Binding PressedForeground, RelativeSource={RelativeSource TemplatedParent}}" />
</MultiTrigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource ControlFillColorDisabledBrush}" />
Expand Down
40 changes: 21 additions & 19 deletions src/Wpf.Ui/Styles/Controls/NavigationView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,19 @@
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<ContentControl
<ContentPresenter
x:Name="Icon"
Grid.Column="0"
Margin="0,0,20,0"
Padding="0"
VerticalAlignment="Center"
Content="{TemplateBinding Icon}"
Focusable="False"
Foreground="{TemplateBinding Foreground}"
RenderTransformOrigin="0.5, 0.5">
<ContentControl.RenderTransform>
RenderTransformOrigin="0.5, 0.5"
TextElement.Foreground="{TemplateBinding Foreground}">
<ContentPresenter.RenderTransform>
<ScaleTransform x:Name="IconScaleTransform" ScaleX="1.0" ScaleY="1.0" />
</ContentControl.RenderTransform>
</ContentControl>
</ContentPresenter.RenderTransform>
</ContentPresenter>

<ContentPresenter Grid.Column="1" Content="{TemplateBinding Content}" />
</Grid>
Expand All @@ -95,7 +94,7 @@
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="{DynamicResource ControlFillColorDisabledBrush}" />
<Setter TargetName="Icon" Property="Foreground" Value="{DynamicResource TextFillColorDisabledBrush}" />
<Setter TargetName="Icon" Property="TextElement.Foreground" Value="{DynamicResource TextFillColorDisabledBrush}" />
</Trigger>
<Trigger Property="Content" Value="{x:Null}">
<Setter TargetName="Icon" Property="Margin" Value="0" />
Expand Down Expand Up @@ -203,15 +202,15 @@
</Grid.ColumnDefinitions>

<Grid Grid.Column="0">
<ContentControl
<ContentPresenter
x:Name="IconContentPresenter"
Margin="-1,0,0,0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="{TemplateBinding Icon}"
Focusable="False"
FontSize="16"
Foreground="{DynamicResource TextFillColorPrimaryBrush}" />
TextElement.FontSize="16"
TextElement.Foreground="{DynamicResource TextFillColorPrimaryBrush}" />
</Grid>

<Rectangle
Expand Down Expand Up @@ -406,7 +405,7 @@
<Condition Property="IsActive" Value="False" />
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter TargetName="IconContentPresenter" Property="Foreground">
<Setter TargetName="IconContentPresenter" Property="TextElement.Foreground">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource TextFillColorPrimary}" />
</Setter.Value>
Expand Down Expand Up @@ -856,18 +855,21 @@
<SolidColorBrush Color="{DynamicResource SystemAccentColorSecondary}" />
</Rectangle.Fill>
</Rectangle>

<Grid HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ContentControl

<ContentPresenter
x:Name="IconContentPresenter"
Grid.Row="0"
Margin="0"
Content="{TemplateBinding Icon}"
FontSize="24"
Foreground="{DynamicResource TextFillColorPrimaryBrush}" />
TextElement.FontSize="24"
TextElement.Foreground="{DynamicResource TextFillColorPrimaryBrush}" />

<Grid
x:Name="ContentGrid"
Grid.Row="1"
Expand All @@ -892,7 +894,7 @@
<SolidColorBrush Color="{DynamicResource ControlFillColorDefault}" />
</Setter.Value>
</Setter>
<Setter TargetName="IconContentPresenter" Property="Foreground">
<Setter TargetName="IconContentPresenter" Property="TextElement.Foreground">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource SystemAccentColorSecondary}" />
</Setter.Value>
Expand Down Expand Up @@ -1119,14 +1121,14 @@
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<ContentControl
<ContentPresenter
x:Name="IconContentPresenter"
Margin="0,0,6,0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="{TemplateBinding Icon}"
FontSize="16"
Foreground="{DynamicResource TextFillColorPrimaryBrush}" />
TextElement.FontSize="16"
TextElement.Foreground="{DynamicResource TextFillColorPrimaryBrush}" />
</Grid>
<ContentPresenter
x:Name="ElementContentPresenter"
Expand Down
8 changes: 5 additions & 3 deletions src/Wpf.Ui/Styles/Controls/NavigationViewItemHeader.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ContentControl

<ContentPresenter
x:Name="IconElement"
Grid.Column="0"
Margin="0,0,4,0"
VerticalAlignment="Center"
Content="{TemplateBinding Icon}"
FontSize="{TemplateBinding FontSize}"
Foreground="{TemplateBinding Foreground}" />
TextElement.FontSize="{TemplateBinding FontSize}"
TextElement.Foreground="{TemplateBinding Foreground}" />

<TextBlock
Grid.Column="1"
FontSize="{TemplateBinding FontSize}"
Expand Down
Loading

0 comments on commit 2b54a5b

Please sign in to comment.