在WPF中可以实现椭圆,利用Ellipse或者Border。但是要实现圆,就必须设定固定的长宽,然后根据长宽设置对应的倒角才可以实现圆。如果这样设计,就固定了长宽,不能根据界面自适应调整。所以我们可以根据绑定和转换器将实际的长宽转换为对应的倒角。
using System;
using System.Globalization;
using System.Windows.Data;
[ValueConversion(typeof(double), typeof(double[]))]
public class RoundMathConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length < 2)
{
throw new NotImplementedException();
}
double.TryParse(values[0].ToString(), out double d1);
double.TryParse(values[1].ToString(), out double d2);
if (d1 * d2 == 0)
{
return double.NaN;
}
return Math.Min(d1, d2);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
[ValueConversion(typeof(System.Windows.CornerRadius), typeof(double[]))]
public class RoundRadiusConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length < 2)
{
throw new NotImpl

本文探讨如何在WPF中使用数据绑定和转换器实现在不同场景下创建自适应圆和椭圆,包括通过RoundMathConverter和RoundRadiusConverter动态设置边角和尺寸,以及在Button模板中实现圆形按钮的动态宽度和高度计算。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



