简体   繁体   中英

any trick to a WPF MinWidth or MinHeight that maintains the Height/Width ratio?

I have the code below that works. It correctly keeps the minimum drawing width at 20 pixels wide. However, I need to set a MinHeight value. I want my MinHeight value to maintain the current Height/Width ratio. How do I do that?

<Grid MinWidth="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type c:IWorldAndScreen}}, Path=MetersPerPixel, Converter={StaticResource multiplier}, ConverterParameter=20}">
    <Grid.Width>
        <MultiBinding Converter="{StaticResource summation}">
            <Binding Path="Front" />
            <Binding Path="Back" />
        </MultiBinding>
    </Grid.Width>
    <Grid.Height>
        <MultiBinding Converter="{StaticResource summation}">
            <Binding Path="Left" />
            <Binding Path="Right" />
        </MultiBinding>
    </Grid.Height>
...
</Grid>

Here's what I came up with:

<Grid.MinHeight>
    <!-- height/width * actualWidth -->
    <MultiBinding Converter="{StaticResource divMulAdd}">
        <Binding RelativeSource="{RelativeSource Self}" Path="Height"/>
        <Binding RelativeSource="{RelativeSource Self}" Path="Width"/>
        <Binding RelativeSource="{RelativeSource Self}" Path="ActualWidth"/>
    </MultiBinding>
</Grid.MinHeight>

Combined with this converter:

public class DivMulAddMultiConverter : IMultiValueConverter
    {
        #region Implementation of IMultiValueConverter

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != typeof(double)) throw new ArgumentException("Expected a target type of Double", "targetType");
            if (values == null || values.Length <= 0) return 0.0;

            var cur = System.Convert.ToDouble(values[0]);
            if(values.Length > 1)
                cur /= System.Convert.ToDouble(values[1]);
            if(values.Length > 2)
                cur *= System.Convert.ToDouble(values[2]);
            for(int i = 3; i < values.Length; i++)
                cur += System.Convert.ToDouble(values[i]);

            return cur;
        }

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

        #endregion
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM