【问题标题】:WPF Image in grid with label and caption带有标签和标题的网格中的 WPF 图像
【发布时间】:2020-02-24 10:55:39
【问题描述】:

我一直在努力将一张上面有标签、下面有标题的图片放在网格中。 我想要的是顶部标签在图像正上方的中间对齐,底部标签在图像正下方,如下所示:

但我实际上得到的是:

在我看来,我使用了以下代码:

<Window x:Class="ImageWithText.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid Margin="5">

        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>       

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="auto"/>
            </Grid.RowDefinitions>

            <Label Content="This is a label" 
                   HorizontalAlignment="Center"
                   VerticalAlignment="Bottom"
                   Grid.Row="0"/>

            <Image x:Name="MainImage"
                   Stretch="Uniform"
                   Grid.Row="1">
                <Image.Source>
                    <BitmapImage UriSource="test.jpg"/>
                </Image.Source>
            </Image>

            <TextBlock HorizontalAlignment="Center"
                       VerticalAlignment="Top"
                       Grid.Row="2">
                    <Run Text="Some text "/>
                    <Run Text="Some more text"/>
            </TextBlock>

        </Grid>
    </Grid>
</Window>

我也尝试过使用 DockPanel:

<Window x:Class="ImageWithText.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid Margin="5">

        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <DockPanel>

            <Label Content="This is a label" HorizontalAlignment="Center" DockPanel.Dock="Top"/>

            <Image x:Name="MainImage"
               Stretch="Uniform"
               DockPanel.Dock="Top">
                <Image.Source>
                    <BitmapImage UriSource="test.jpg"/>
                </Image.Source>
            </Image>

            <TextBlock Margin="5"
                   HorizontalAlignment="Center"
                   DockPanel.Dock="Top">
                    <Run Text="Some text "/>
                    <Run Text="Some more text"/>
            </TextBlock>

        </DockPanel>

    </Grid>
</Window>

但如果窗口不够大,则标题不可见。

我已经尝试了 LastChildFill 的两个选项,但没有任何区别。有谁知道如何完成这项工作?

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    您的内部网格被拉伸以填充整个外部网格。由于内部网格中的中间行是唯一具有可变大小的行,因此它会垂直扩展,但是由于图像默认为“Stretch”=“Uniform”,它只填充了中间行的一定量并且垂直在它所在行的中间对齐。

    这应该对其进行排序:

    <Grid Margin="5">
            <Grid x:Name="LayoutGrid" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center">            
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="auto"/>
                </Grid.RowDefinitions>
    
                <Label Content="This is a label" 
                   HorizontalAlignment="Center"
                   VerticalAlignment="Bottom"
                   Grid.Row="0"/>
    
            <Image x:Name="MainImage"
                   Stretch="Uniform"
                   Grid.Row="1">
                <Image.Source>
                   <BitmapImage UriSource="test.jpg"/>
                </Image.Source>
            </Image>
    
            <TextBlock HorizontalAlignment="Center"
                       VerticalAlignment="Top"
                       Grid.Row="2">
                    <Run Text="Some text "/>
                    <Run Text="Some more text"/>
            </TextBlock>
    
        </Grid>
    </Grid>
    

    调试此类问题的一个好方法是设置各种元素的背景颜色以查看它们所占据的区域。

    编辑以反映评论

    【讨论】:

    • 感谢背景色提示,非常好用。我尝试了你的建议,但如果窗口宽度太小,标签会像here 一样远离图像(上下)。有没有办法解决这个问题?
    • 我已经编辑了上述解决方案来解决您的评论。现在,当内部网格没有填充任一方向的可用空间时,它只是水平和垂直居中。这可确保文本与图像直接相邻。
    • 是的,就是这样!将其标记为解决方案,谢谢!
    【解决方案2】:

    您需要将网格行定义反转为:

    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    

    这是因为你的中间网格被拉伸到剩下的任何区域,所以你会得到两个标签都在图像远端的印象。

    【讨论】:

    • 感谢您的快速响应。我尝试了你的建议,但如果我调整窗口的高度小于图像所需的高度,它不会缩放:example
    猜你喜欢
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 2012-11-07
    相关资源
    最近更新 更多