How to keep a nested grid square without using ViewBox?
这类似于how-do-i-keep-aspect-ratio-on-scalable-scrollable-content-in-wpf,但有以下区别:
您只需要将参数
1 | <Image x:Name="image" Height="{Binding Width, ElementName=image}"/> |
您应该将 Grid 的 Width 和 Height 绑定到一个值:
1 2 | <!--Dont forget to specify source where MaxSizeParam lies--> <Grid Width="{Binding MaxSizeParam}" Height="{Binding MaxSizeParam}"/> |
MaxSizeParam 您可以在任何地方以您想要的方式提供。例如,如果网格有 Button,那么在 Button 的 SizeChanged 事件上,您应该重新计算 MaxSizeParam:
1 2 3 4 5 6 | void button_SizeChanged(object sender, SizeChangedEventArgs e) { MaxSizeParam = e.NewSize.Width > e.NewSize.Height ? e.NewSize.Width : e.NewSize.Height; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("MaxSizeParam")); } |