【问题标题】:Change stackpanel's items height a and width based on window size根据窗口大小更改堆栈面板的项目高度 a 和宽度
【发布时间】:2018-07-16 17:53:18
【问题描述】:

我想制作一个包含 3 个区域的简单 UI,我希望它们如图所示,每个区域使用大约 33% 的 Window 高度:

我能够做到这个宽度 Grid 和他的 RowDefinitions 但问题是我希望这三个区域根据窗口宽度改变方向,所以我认为使用 StackPanel 而不是 Grid 并将他的“方向”属性更改为“水平”当窗口更大时可能是解决方案。但是现在我面临其他问题,我不知道为每个自动更改的区域设置高度或宽度很热,因为我不能在 Grid.RowDefinitions 中为每个区域使用“0.3*”。

你知道如何实现这个用户界面吗?

谢谢!

编辑:好的,基于这里的评论是我的实际代码:

<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>

【问题讨论】:

  • 当问一个由你的代码引起的问题时,如果你提供人们可以用来重现问题的代码,你会得到更好的答案Read the help
  • @zatamine 感谢您的提示,我已添加。有什么想法吗?

标签: xaml user-interface uwp uwp-xaml stackpanel


【解决方案1】:

这是 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>

下面是代码:

        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;
            }
        }

享受调整窗口大小的乐趣。

【讨论】:

  • 您好,谢谢!这对我来说很有效,但只有当我把窗口变大时,调整它的大小是行不通的。我该如何实现这种行为?
  • 糟糕...稍微更改 XAML。将StackPanel 包装成Grid 并将SizeChanged="Stack_OnSizeChanged" 移动到它。
猜你喜欢
  • 2015-03-22
  • 2012-06-19
  • 1970-01-01
  • 2017-04-15
  • 2016-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-11
相关资源
最近更新 更多