wpf 为什么第二个网格不显示它的任何子网格?C#

lymgl2op  于 2023-03-13  发布在  C#
关注(0)|答案(1)|浏览(167)

首先,我应该说我不是编程新手,但对XAML非常陌生,所以这可能是一个相当简单的问题。

<Grid x:Name="motivGrid" HorizontalAlignment="Center" Width="700" Margin="46,200,46,289" Height="100">
            <Label Content="How's your motivation?" HorizontalAlignment="Center" Margin="150,0,150,50" FontFamily="Yu Gothic Light" FontSize="30" Height="50" VerticalAlignment="Center" Width="306"/>
            <Slider x:Name="motivSlider" HorizontalAlignment="Center" Margin="0,48,0,8" Width="700" Height="34" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Visible" BorderThickness="1" RenderTransformOrigin="0.5,0.5" Foreground="#FF5377C5" Background="#FF86F78B" BorderBrush="#FF31B436" Value="-1" Cursor="Hand" VerticalAlignment="Center" LargeChange="0.1" TickPlacement="Both" AutoToolTipPlacement="TopLeft" TickFrequency="0.1"/>
        </Grid>

        <Grid x:Name="energyGrid" HorizontalAlignment="Center" Width="700" Margin="46,320,46,289" Height="100">
            <Label Content="How's your energy levels?" HorizontalAlignment="Center" Margin="150,0,150,50" FontFamily="Yu Gothic Light" FontSize="30" Height="50"  VerticalAlignment="Center" Width="306"/>
            <Slider x:Name="energySlider" HorizontalAlignment="Center" Margin="0,48,0,8" Width="700" Height="34" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Visible" BorderThickness="1" RenderTransformOrigin="0.5,0.5" Foreground="#FF5377C5" Background="#FF86F78B" BorderBrush="#FF31B436" Value="-1" Cursor="Hand" VerticalAlignment="Center" LargeChange="0.1" TickPlacement="Both" AutoToolTipPlacement="TopLeft" TickFrequency="0.1"/>
        </Grid>

两个网格除了名称和y值位置外完全相同,为什么第一个网格显示良好,而第二个网格的所有元素都不可见?
边注:我使用网格纯粹是作为每个滑块和标签的容器,有没有更好的等价物说HTML的<div>函数?我有父网格,我没有包括,但工作正常,足够大,以允许丢失的网格显示。

z2acfund

z2acfund1#

当您使用网格进行布局时,您应该考虑到网格由一些(虚拟)单元格组成,这些单元格可用于放置控件。如果根本不指定任何相关信息,则会将网格中的元素放置在同一单元格中,并且您很想通过坐标来排列控件。一旦屏幕分辨率发生变化,这将造成严重破坏。
在@PaulF发布的wpf教程链接中提到了重要的选择,但信息相当压缩,对于您的情况,我要么使用嵌套网格,要么使用StackPanel。
对于下面的代码,我假设您已经将两个网格放置在一个中,那么Grid变体可能如下所示

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid Grid.Row="0" x:Name="motivGrid" HorizontalAlignment="Center"  >
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Label Grid.Row="0" Content="How's your motivation?" HorizontalAlignment="Center" FontFamily="Yu Gothic Light" FontSize="30" Height="50" VerticalAlignment="Center" Width="306"/>
        <Slider Grid.Row="1" x:Name="motivSlider" HorizontalAlignment="Center" Width="100" Height="34" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Visible" BorderThickness="1" RenderTransformOrigin="0.5,0.5" Foreground="#FF5377C5" Background="#FF86F78B" BorderBrush="#FF31B436" Value="-1" Cursor="Hand" VerticalAlignment="Center" LargeChange="0.1" TickPlacement="Both" AutoToolTipPlacement="TopLeft" TickFrequency="0.1"/>
    </Grid>

    <Grid Grid.Row="1" x:Name="energyGrid" HorizontalAlignment="Center" >
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Label Grid.Row="0"  Content="How's your energy levels?" HorizontalAlignment="Center" FontFamily="Yu Gothic Light" FontSize="30" Height="50"  VerticalAlignment="Center" Width="306"/>
        <Slider Grid.Row="1" x:Name="energySlider" HorizontalAlignment="Center" Width="100" Height="34" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Visible" BorderThickness="1" RenderTransformOrigin="0.5,0.5" Foreground="#FF5377C5" Background="#FF86F78B" BorderBrush="#FF31B436" Value="-1" Cursor="Hand" VerticalAlignment="Center" LargeChange="0.1" TickPlacement="Both" AutoToolTipPlacement="TopLeft" TickFrequency="0.1"/>
    </Grid>
</Grid>

如果没有特殊原因使motivoGrid和energyGrid具有唯一名称,则将存档相同的布局,仅具有4行的最外层网格。
假设这些网格名称不用于其他目的,则备选名称可以是:

<StackPanel Orientation="Vertical">
        <Label Content="How's your motivation?" HorizontalAlignment="Center" FontFamily="Yu Gothic Light" FontSize="30" Height="50" VerticalAlignment="Center" Width="306"/>
        <Slider x:Name="motivSlider" HorizontalAlignment="Center" Width="100" Height="34" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Visible" BorderThickness="1" RenderTransformOrigin="0.5,0.5" Foreground="#FF5377C5" Background="#FF86F78B" BorderBrush="#FF31B436" Value="-1" Cursor="Hand" VerticalAlignment="Center" LargeChange="0.1" TickPlacement="Both" AutoToolTipPlacement="TopLeft" TickFrequency="0.1"/>
        <Label  Content="How's your energy levels?" HorizontalAlignment="Center" FontFamily="Yu Gothic Light" FontSize="30" Height="50"  VerticalAlignment="Center" Width="306"/>
        <Slider x:Name="energySlider" HorizontalAlignment="Center" Width="100" Height="34" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Visible" BorderThickness="1" RenderTransformOrigin="0.5,0.5" Foreground="#FF5377C5" Background="#FF86F78B" BorderBrush="#FF31B436" Value="-1" Cursor="Hand" VerticalAlignment="Center" LargeChange="0.1" TickPlacement="Both" AutoToolTipPlacement="TopLeft" TickFrequency="0.1"/>
</StackPanel>

相关问题