C# 对WPF MinWidth或MinHeight有什么保持高宽比的技巧吗?
我有下面的代码可以工作。它正确地将最小图形宽度保持在20像素宽。但是,我需要设置一个最小高度值。我希望我的最小高度值保持当前的高宽比。我该怎么做C# 对WPF MinWidth或MinHeight有什么保持高宽比的技巧吗?,c#,wpf,data-binding,C#,Wpf,Data Binding,我有下面的代码可以工作。它正确地将最小图形宽度保持在20像素宽。但是,我需要设置一个最小高度值。我希望我的最小高度值保持当前的高宽比。我该怎么做 <Grid MinWidth="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type c:IWorldAndScreen}}, Path=MetersPerPixel, Converter={StaticResource multiplier}, Con
<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
}
公共类divmuladMultiConverter:IMultiValueConverter
{
#IMultiValueConverter的区域实现
公共对象转换(对象[]值,类型targetType,对象参数,CultureInfo区域性)
{
if(targetType!=typeof(double))抛出新的ArgumentException(“预期目标类型为double”,“targetType”);
如果(值==null | |值。长度1)
cur/=System.Convert.ToDouble(值[1]);
如果(值.Length>2)
cur*=System.Convert.ToDouble(值[2]);
for(int i=3;i