-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(GH-4050) Add CornerRadiusFilterConverter
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
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/MahApps.Metro/Converters/CornerRadiusFilterConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |