Ashish Kasma
Ashish Kasma

Reputation: 3642

How to resize all user control when parent user control resized

How to resize all user control when parent user control resized

Application has one parent user control, which can be resized to make it small and large. now but the same user control has 3/4 more user control. Two user controls has no size as fixed, but they are dynamically do some drawing.

Upvotes: 0

Views: 845

Answers (1)

Rachel
Rachel

Reputation: 132558

Specify sizes in a Percent instead of hardcoding them.

An easy way to do that is create a Converter that takes the parent size as the binding and has a percent value as the parameter.

For example, converter would be something like this:

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();
    }
}

And the XAML would say something like this:

<UserControl x:Name=RootControl>
    <Button Height="{Binding ElementName=RootControl, Path=Height, 
            Converter={StaticResource MyPercentToDoubleConverter}, ConverterParameter=.2}" />
</UserControl>     

Upvotes: 1

Related Questions