为什么不让我的按钮更改高度,而仅通过Auto属性更改

时间:2018-08-28 10:27:50

标签: c# .net wpf xaml

所以我有一个带有几个按钮,图像等的测试应用程序。 当我左右移动窗口时,控件会被压在一起。 现在,如果我上下移动。尤其是向上移动时,它们不会变薄,我想您会称呼它。这是为什么?我尝试将高度属性设置为自动。它什么也没做。对于Image也是一样,如果仅左右并排,则不会缩小。 GIF Showing what happens

<Grid>
    <Grid.RowDefinitions >
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>

    <Grid Grid.Row="0"
          Grid.Column="0"
          Background="Gray">


        <StackPanel Height="auto">
            <TextBlock Text="Image Preview"
                       HorizontalAlignment="Center"
                       Height="25"
                       Width="auto"/>

            <Image Source="{Binding ProcessImage.PreviewImage}"
                       Height="auto"
                   Width="auto"
                       RenderOptions.BitmapScalingMode="Fant"
                       Stretch="Uniform"/>


            <ComboBox SelectedIndex="{Binding ProcessImage.SelectedIndex, Mode=TwoWay}"
                      Height="auto"
                      Width="auto"
                      VerticalAlignment="Bottom">
                <ComboBoxItem Content="North east"/>
                <ComboBoxItem Content="North west"/>
                <ComboBoxItem Content="South east"/>
                <ComboBoxItem Content="South west"/>
                <ComboBoxItem Content="Center"/>
            </ComboBox>
        </StackPanel>

    </Grid>

    <Grid Grid.Row="0"
          Grid.Column="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <StackPanel Grid.Column="1">
            <Button Content="Preview"
                    Width="auto"
                    Height="30"
                    Command="{Binding PreviewImageCommand}"/>

            <Button Width="auto"
                    Height="30"
                    Content="Directory"
                    Command="{Binding AddDirectoryCommand}"/>

            <TextBox Width="auto"
                     Height="auto"
                     Text="{Binding ProcessImage.Directory, Mode=TwoWay}"/>

            <Button Width="auto"
                    Height="30"
                    Content="Watermark"
                    Command="{Binding AddWatermarkDirectoryCommand}"/>

            <TextBox Width="auto"
                     Height="auto"
                     Text="{Binding ProcessImage.WatermarkImage}"/>

            <Button Width="auto"
                    Height="30"
                    Content="Process"
                    Command="{Binding ProcessCommand}"
                    Margin="0,15,0,0"/>
        </StackPanel>
    </Grid>
</Grid>

1 个答案:

答案 0 :(得分:1)

  

那是为什么?

因为包含您的图像的网格具有两行定义为每行占据网格高度的一半。如gif所示,网格行实际上变薄了

您的问题是包裹在图片周围的StackPanel。它不会缩小图像的内部,因此最好将图像直接放入网格中。