Xaml 更改stackpanel';s基于窗口大小的项目高度和宽度
我想制作一个包含3个区域的简单UI,我希望它们如图所示,每个区域使用大约33%的窗口高度: 我可以用网格和他的行定义来定义宽度,但问题是我希望这三个区域根据窗口宽度改变方向,所以我认为使用StackPanel代替网格,并在窗口较大时将其“方向”属性更改为“水平”可能是解决方案。但现在我面临着另一个问题,我不知道如何为自动更改的每个区域设置高度或宽度,因为我不能对Grid.RowDefinitions中的每个like使用“0.3*” 你知道如何实现这个UI吗 谢谢 编辑:好的,根据评论,这里是我的实际代码:Xaml 更改stackpanel';s基于窗口大小的项目高度和宽度,xaml,user-interface,uwp,uwp-xaml,stackpanel,Xaml,User Interface,Uwp,Uwp Xaml,Stackpanel,我想制作一个包含3个区域的简单UI,我希望它们如图所示,每个区域使用大约33%的窗口高度: 我可以用网格和他的行定义来定义宽度,但问题是我希望这三个区域根据窗口宽度改变方向,所以我认为使用StackPanel代替网格,并在窗口较大时将其“方向”属性更改为“水平”可能是解决方案。但现在我面临着另一个问题,我不知道如何为自动更改的每个区域设置高度或宽度,因为我不能对Grid.RowDefinitions中的每个like使用“0.3*” 你知道如何实现这个UI吗 谢谢 编辑:好的,根据评论,这里是我
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="500"/>
</VisualState.StateTriggers>
<VisualState.Setters>
// Changes on orientation
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid.RowDefinitions>
<RowDefinition Height="0.35*"/>
<RowDefinition Height="0.30*"/>
<RowDefinition Height="0.35*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="Green">
</Grid>
<Grid Grid.Row="1">
</Grid>
<Grid Grid.Row="2" Background="Blue">
</Grid>
</Grid>
//方向的变化
以下是XAML:
<Grid SizeChanged="Stack_OnSizeChanged">
<StackPanel Orientation="{x:Bind Orientation, Mode=OneWay}">
<Rectangle Height="{x:Bind PercentHeight, Mode=OneWay}" Width="{x:Bind PercentWidth, Mode=OneWay}" Fill="Lime"/>
<Rectangle Height="{x:Bind PercentHeight, Mode=OneWay}" Width="{x:Bind PercentWidth, Mode=OneWay}" Fill="DeepPink"/>
<Rectangle Height="{x:Bind PercentHeight, Mode=OneWay}" Width="{x:Bind PercentWidth, Mode=OneWay}" Fill="DeepSkyBlue"/>
</StackPanel>
</Grid>
享受调整窗口大小的乐趣。当您询问由代码引起的问题时,如果您提供人们可以用来重现问题的代码@zatamine,您会得到更好的答案谢谢您的提示,我已经添加了它。有什么主意吗?嗨,谢谢!这对我来说很有用,但只有当我把窗口放大时,缩小它的尺寸才起作用。我该如何实施这种行为?哎呀。。。稍微更改一下XAML。将
StackPanel
包装到网格中
并将SizeChanged=“Stack\u OnSizeChanged”
移动到网格中。
public static readonly DependencyProperty PercentHeightProperty = DependencyProperty.Register(
"PercentHeight", typeof(double), typeof(MyUserControl1), new PropertyMetadata(default(double)));
public double PercentHeight
{
get => (double) GetValue(PercentHeightProperty);
set => SetValue(PercentHeightProperty, value);
}
public static readonly DependencyProperty PercentWidthProperty = DependencyProperty.Register(
"PercentWidth", typeof(double), typeof(MyUserControl1), new PropertyMetadata(default(double)));
public double PercentWidth
{
get => (double) GetValue(PercentWidthProperty);
set => SetValue(PercentWidthProperty, value);
}
public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(
"Orientation", typeof(Orientation), typeof(MyUserControl1), new PropertyMetadata(default(Orientation)));
public Orientation Orientation
{
get => (Orientation) GetValue(OrientationProperty);
set => SetValue(OrientationProperty, value);
}
private int _count = 3;
public MyUserControl1()
{
InitializeComponent();
}
private void Stack_OnSizeChanged(object sender, SizeChangedEventArgs e)
{
Orientation = e.NewSize.Width > 512 ? Orientation.Horizontal : Orientation.Vertical;
if (Orientation == Orientation.Horizontal)
{
PercentHeight = e.NewSize.Height;
PercentWidth = e.NewSize.Width / _count;
}
else
{
PercentHeight = e.NewSize.Height / _count;
PercentWidth = e.NewSize.Width;
}
}