Skip to content

Commit

Permalink
(GH-4050) Add CornerRadiusFilterConverter
Browse files Browse the repository at this point in the history
Filters a CornerRadius by the given Filter property. Result can be a new CornerRadius or a value of it's 4 corners.
  • Loading branch information
punker76 committed Feb 28, 2021
1 parent 04729f7 commit b204632
Showing 1 changed file with 63 additions and 0 deletions.
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;
}
}
}

0 comments on commit b204632

Please sign in to comment.