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

Fix CheckBox CornerRadius not working #4056

Merged
merged 2 commits into from
Feb 28, 2021
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 @@ -321,7 +321,7 @@
</StackPanel.Resources>

<Label Content="CheckBox Win10" Style="{DynamicResource DescriptionHeaderStyle}" />
<CheckBox IsChecked="False" IsEnabled="True" />
<CheckBox IsChecked="False" IsEnabled="True" mah:CheckBoxHelper.CheckCornerRadius="2" />
<CheckBox IsChecked="False" IsEnabled="False" />
<CheckBox IsChecked="True" IsEnabled="False" />
<CheckBox IsChecked="True" IsEnabled="True" />
Expand Down
30 changes: 30 additions & 0 deletions src/MahApps.Metro/Controls/Helper/CheckBoxHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,36 @@ public static void SetCheckSize(DependencyObject obj, double value)
obj.SetValue(CheckSizeProperty, value);
}

public static readonly DependencyProperty CheckCornerRadiusProperty
= DependencyProperty.RegisterAttached(
"CheckCornerRadius",
typeof(CornerRadius),
typeof(CheckBoxHelper),
new FrameworkPropertyMetadata(
new CornerRadius(),
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender));

/// <summary>
/// Gets the CornerRadius of the CheckBox itself.
/// The CheckCornerRadius property allows users to control the roundness of the CheckBox corners independently by setting a radius value for each corner.
/// </summary>
[Category(AppName.MahApps)]
[AttachedPropertyBrowsableForType(typeof(CheckBox))]
public static CornerRadius GetCheckCornerRadius(UIElement element)
{
return (CornerRadius)element.GetValue(CheckCornerRadiusProperty);
}

/// <summary>
/// Sets the CornerRadius of the CheckBox itself.
/// </summary>
[Category(AppName.MahApps)]
[AttachedPropertyBrowsableForType(typeof(CheckBox))]
public static void SetCheckCornerRadius(UIElement element, CornerRadius value)
{
element.SetValue(CheckCornerRadiusProperty, value);
}

public static readonly DependencyProperty CheckStrokeThicknessProperty
= DependencyProperty.RegisterAttached(
"CheckStrokeThickness",
Expand Down
63 changes: 63 additions & 0 deletions src/MahApps.Metro/Converters/CornerRadiusFilterConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace MahApps.Metro.Converters
{
/// <summary>
/// Filters a CornerRadius by the given Filter property. Result can be a new CornerRadius or a value of it's 4 corners.
/// </summary>
public class CornerRadiusFilterConverter : IValueConverter
{
public RadiusType Filter { get; set; } = RadiusType.None;

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is CornerRadius cornerRadius)
{
var filter = this.Filter;

// yes, we can override it with the parameter value
if (parameter is RadiusType radiusType)
{
filter = radiusType;
}

switch (filter)
{
default:
return cornerRadius;
case RadiusType.Left:
return new CornerRadius(cornerRadius.TopLeft, 0, 0, cornerRadius.BottomLeft);
case RadiusType.Top:
return new CornerRadius(cornerRadius.TopLeft, cornerRadius.TopRight, 0, 0);
case RadiusType.Right:
return new CornerRadius(0, cornerRadius.TopRight, cornerRadius.BottomRight, 0);
case RadiusType.Bottom:
return new CornerRadius(0, 0, cornerRadius.BottomRight, cornerRadius.BottomLeft);
case RadiusType.TopLeft:
return cornerRadius.TopLeft;
case RadiusType.TopRight:
return cornerRadius.TopRight;
case RadiusType.BottomRight:
return cornerRadius.BottomRight;
case RadiusType.BottomLeft:
return cornerRadius.BottomLeft;
}
}

return Binding.DoNothing;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// for now no back converting
return DependencyProperty.UnsetValue;
}
}
}
7 changes: 6 additions & 1 deletion src/MahApps.Metro/Styles/Controls.CheckBox.xaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mah="clr-namespace:MahApps.Metro.Controls"
xmlns:system="clr-namespace:System;assembly=mscorlib">
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:converters="clr-namespace:MahApps.Metro.Converters">

<!-- ********************************** CheckBoxStyle ********************************** -->
<system:Double x:Key="CheckBoxBorderThemeThickness">2</system:Double>
<system:Double x:Key="CheckBoxCheckedStrokeThickness">0</system:Double>
<converters:CornerRadiusFilterConverter x:Key="CornerRadiusTopLeftConverter" Filter="TopLeft" />
<converters:CornerRadiusFilterConverter x:Key="CornerRadiusBottomRightConverter" Filter="BottomRight" />

<Style x:Key="MahApps.Styles.CheckBox" TargetType="CheckBox">
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.CheckBox.BackgroundUnchecked}" />
Expand Down Expand Up @@ -42,6 +45,8 @@
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<Rectangle x:Name="NormalRectangle"
Fill="{TemplateBinding mah:CheckBoxHelper.CheckBackgroundFillUnchecked}"
RadiusX="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(mah:CheckBoxHelper.CheckCornerRadius), Mode=OneWay, Converter={StaticResource CornerRadiusTopLeftConverter}}"
RadiusY="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(mah:CheckBoxHelper.CheckCornerRadius), Mode=OneWay, Converter={StaticResource CornerRadiusBottomRightConverter}}"
Stroke="{TemplateBinding mah:CheckBoxHelper.CheckBackgroundStrokeUnchecked}"
StrokeThickness="{TemplateBinding mah:CheckBoxHelper.CheckStrokeThickness}"
UseLayoutRounding="False" />
Expand Down