[英]Calculate Width and Height respecting Aspect Ratio for any given Max Width and Max Height
[英]any trick to a WPF MinWidth or MinHeight that maintains the Height/Width ratio?
我有下面的代码可以工作。 它将最小绘图宽度正确地保持在20像素宽。 但是,我需要设置一个MinHeight值。 我希望我的MinHeight值保持当前的高度/宽度比。 我怎么做?
<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>
这是我想出的:
<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>
与此转换器结合:
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.