Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a way to use DataTemplate in BadgedControl #3790

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,15 @@
<Button Content="Print" />
</Controls:Badged>
<Controls:Badged x:Name="CountingBadge"
BadgeFontSize="12"
BadgeMargin="4 0"
Width="100"
Margin="{StaticResource ControlMargin}">
<Controls:Badged.BadgeTemplate>
<DataTemplate>
<TextBlock Text="{Binding StringFormat='{}Click-count: {0:00}'}" />
</DataTemplate>
</Controls:Badged.BadgeTemplate>
<Button Click="CountingButton_OnClick" Content="Click Me" />
</Controls:Badged>
<Button Width="100"
Expand Down
31 changes: 31 additions & 0 deletions src/MahApps.Metro/Converters/SizeToCornerRadiusConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;

namespace MahApps.Metro.Converters
{
/// <summary>
/// This Converter converts a given height or width of an control to a CornerRadius
/// </summary>
[ValueConversion(typeof(double), typeof(CornerRadius))]
[MarkupExtensionReturnType(typeof(SizeToCornerRadiusConverter))]
public class SizeToCornerRadiusConverter : MarkupConverter
{
protected override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is double val ? new CornerRadius(val / 2) : new CornerRadius();
}

protected override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Binding.DoNothing;
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}
22 changes: 17 additions & 5 deletions src/MahApps.Metro/Themes/Badged.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:MahApps.Metro.Converters"
xmlns:controls="clr-namespace:MahApps.Metro.Controls">

<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
Expand All @@ -22,7 +23,10 @@
<Style TargetType="{x:Type controls:Badged}">
<Setter Property="BadgeBackground" Value="{DynamicResource MahApps.Brushes.Badged.Background}" />
<Setter Property="BadgeChangedStoryboard" Value="{StaticResource BadgeChangedStoryboard}" />
<Setter Property="BadgeFontSize" Value="11" />
<Setter Property="BadgeFontWeight" Value="DemiBold" />
<Setter Property="BadgeForeground" Value="{DynamicResource MahApps.Brushes.Badged.Foreground}" />
<Setter Property="BadgeMargin" Value="1 0" />
<Setter Property="BadgePlacementMode" Value="TopRight" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="IsTabStop" Value="False" />
Expand All @@ -43,26 +47,34 @@
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border>
<Border x:Name="PART_BadgeContainer"
MinWidth="18"
MinWidth="{Binding RelativeSource={RelativeSource Mode=Self}, Path=ActualHeight}"
MinHeight="18"
Padding="2"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Background="{TemplateBinding BadgeBackground}"
CornerRadius="9"
BorderBrush="{TemplateBinding BadgeBorderBrush}"
BorderThickness="{TemplateBinding BadgeBorderThickness}"
CornerRadius="{Binding RelativeSource={RelativeSource Mode=Self}, Path=ActualHeight, Converter={converters:SizeToCornerRadiusConverter}}"
RenderTransformOrigin=".5,.5"
TextElement.FontSize="11"
TextElement.FontWeight="DemiBold"
Visibility="{TemplateBinding IsBadgeSet, Converter={StaticResource BooleanToVisibilityConverter}}">
<Border.RenderTransform>
<ScaleTransform ScaleX="1" ScaleY="1" />
</Border.RenderTransform>
<ContentControl x:Name="PART_BadgeContent"
Margin="1 0 1 0"
Margin="{TemplateBinding BadgeMargin}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="{TemplateBinding Badge}"
ContentStringFormat="{TemplateBinding BadgeStringFormat}"
ContentTemplate="{TemplateBinding BadgeTemplate}"
ContentTemplateSelector="{TemplateBinding BadgeTemplateSelector}"
Focusable="False"
FontFamily="{TemplateBinding BadgeFontFamily}"
FontSize="{TemplateBinding BadgeFontSize}"
FontStretch="{TemplateBinding BadgeFontStretch}"
FontStyle="{TemplateBinding BadgeFontStyle}"
FontWeight="{TemplateBinding BadgeFontWeight}"
Foreground="{TemplateBinding BadgeForeground}"
IsTabStop="False" />
</Border>
Expand Down