行自动调整不起作用

时间:2016-02-17 11:26:18

标签: c# wpf grid row

我已经定义了一个包含行和列的网格:

<Grid Grid.Column="0">
  <Grid.RowDefinitions>
    <RowDefinition Height="*"/>
    <RowDefinition Height="Auto"/>
  </Grid.RowDefinitions>

  <DataGrid Name="dtgPPtab1" Grid.Row="0" FontSize="24" Background="{x:Null}" BorderBrush="Gainsboro" BorderThickness="5" Margin="10" AutoGeneratingColumn="Datagrid_AutoGeneratingColumn" SelectionChanged="Datagrid_SelectionChanged" ></DataGrid>
  <StackPanel Name="spPPtab1" Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center">
    <Button Name="btPPPlayPause"  Content="&#xf144;" Foreground="Lime" FontSize="{StaticResource TEXTBOX_BIGBUTTON_FONTSIZE}"  Background="{x:Null}" BorderBrush="{x:Null}" Click="Button_Click"/>
    <Button Name="btPPStop"  Content="&#xF28D;" Visibility="Hidden" Foreground="Red" FontSize="{StaticResource TEXTBOX_BIGBUTTON_FONTSIZE}"  Background="{x:Null}" BorderBrush="{x:Null}" Click="Button_Click"/>
  </StackPanel>
  </Grid>

现在我想通过这样的代码在运行时设置按钮btPPPlayPause:

  1. 从隐藏到可见
  2. 设置维度`btPPPlayPause.Width = btPPPlayPause.Height = ...
  3. 据我所知,rowdefinition height = auto应该允许正确的可视化。相反,我看到的是:

    1. 在设计时此:
    2. enter image description here

      1. 在运行时这个:
      2. enter image description here

        因此自动适应不起作用。 谁能告诉我为什么?

1 个答案:

答案 0 :(得分:2)

每个按钮都有一个内部填充。 此外,您没有设置按钮的字体大小和堆栈面板的高度。

 spPPtab1.Height = easyRunData.FontSize * 2 + btPPPlayPause.Padding.Top + btPPPlayPause.Padding.Bottom;
 btPPPlayPause.Width = btPPPlayPause.Height = (easyRunData.FontSize) * 2 + btPPPlayPause.Padding.Left + btPPPlayPause.Padding.Right;
 btPPPlayPause.FontSize = easyRunData.FontSize*2;

和xaml

<StackPanel Name="spPPtab1" Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0" Background="Red">
 <Button Name="btPPPlayPause" Grid.Row="2" Content="&#xf144;" Foreground="Lime" Background="Blue" BorderBrush="Gainsboro"  FontSize="{StaticResource TEXTBOX_BIGBUTTON_FONTSIZE}" VerticalContentAlignment="Stretch"  VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Padding="5" HorizontalContentAlignment="Stretch" Margin="0" Click="Button_Click">
...

enter image description here