WPF 组件的宽高绑定另一个组件的宽高的指定比值

0.此方法比较适用于响应式界面,组件的大小需要根据窗体大小改变。

1.创建转换函数,并传入比值

    public class SizeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is double d && parameter is string ratioString && double.TryParse(ratioString, out double ratio))
            {
                return d * ratio; // 根据比例计算
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

2.XAML调用

 
 

你可能感兴趣的:(WPF,wpf,开发语言,c#)