Wpf 调整父用户控件大小时如何调整所有用户控件的大小

Wpf 调整父用户控件大小时如何调整所有用户控件的大小,wpf,Wpf,调整父用户控件大小时如何调整所有用户控件的大小 应用程序有一个父用户控件,可以调整其大小使其变小或变大。 现在,同一个用户控件又多了3/4个用户控件。两个用户控件没有固定的大小,但它们是动态绘制的。以百分比指定大小,而不是硬编码 一种简单的方法是创建一个转换器,该转换器以父级大小作为绑定,并以百分比值作为参数 例如,转换器应该是这样的: public class PercentToDoubleConverter : IValueConverter { public object Conv

调整父用户控件大小时如何调整所有用户控件的大小

应用程序有一个父用户控件,可以调整其大小使其变小或变大。
现在,同一个用户控件又多了3/4个用户控件。两个用户控件没有固定的大小,但它们是动态绘制的。

以百分比指定大小,而不是硬编码

一种简单的方法是创建一个转换器,该转换器以父级大小作为绑定,并以百分比值作为参数

例如,转换器应该是这样的:

public class PercentToDoubleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        double size = (double)value;
        double percent = (parameter == null ? 0.00 : System.Convert.ToDouble(parameter));
        return percent * size;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
<UserControl x:Name=RootControl>
    <Button Height="{Binding ElementName=RootControl, Path=Height, 
            Converter={StaticResource MyPercentToDoubleConverter}, ConverterParameter=.2}" />
</UserControl>     
XAML会这样说:

public class PercentToDoubleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        double size = (double)value;
        double percent = (parameter == null ? 0.00 : System.Convert.ToDouble(parameter));
        return percent * size;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
<UserControl x:Name=RootControl>
    <Button Height="{Binding ElementName=RootControl, Path=Height, 
            Converter={StaticResource MyPercentToDoubleConverter}, ConverterParameter=.2}" />
</UserControl>