Viewbox填充所有列网格空间WPF

问题描述 投票:0回答:1

我正在创建一个程序来告诉用户他的帐户余额和类似的信息。我在带有 ViewBox 的网格上显示此信息(因此可以将其大小调整为任何屏幕分辨率),问题是 ViewBox 没有填充网格处的空间。这是我的代码:

<Grid ShowGridLines="True">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="0.5*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="0.5*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Viewbox Grid.Row="0" Grid.Column="1" Stretch="Uniform" HorizontalAlignment="Right" VerticalAlignment="Stretch">
                            <TextBlock Foreground="#5a5a5a"  TextAlignment="Center" Margin="1">Subtotal</TextBlock>
                        </Viewbox>
                        <Viewbox Grid.Row="0" Grid.Column="2" Stretch="Uniform" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                            <TextBox Margin="5" Text="155.60" TextAlignment="Center" IsReadOnly="True"/>
                        </Viewbox>
                        <Viewbox Grid.Row="1" Grid.Column="1" Stretch="Uniform" HorizontalAlignment="Right" VerticalAlignment="Stretch">
                            <TextBlock Foreground="#5a5a5a"  TextAlignment="Center" Margin="1">Descuentos</TextBlock>
                        </Viewbox>
                        <Viewbox Grid.Row="1" Grid.Column="2" Stretch="Uniform" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                            <TextBox Margin="5" Text="0.0" TextAlignment="Center"/>
                        </Viewbox>
</Grid>

结果如下:

如您所见,“折扣”文本框比上面的小,我需要它们具有相同的宽度和高度。我怎样才能做到这一点?我将所有内容放入 ViewBox 中以便为我调整大小,但这样正确吗?我已经尝试过几种方法,例如this,但它使文本变得非常小。

c# wpf xaml viewbox
1个回答
1
投票

您根本不需要视图框:

    <TextBlock Grid.Row="0" Grid.Column="1"  Foreground="#5a5a5a" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"  TextAlignment="Center" Margin="1">Subtotal</TextBlock>
    <TextBox Grid.Row="0" Grid.Column="2"  Margin="5" Text="155.60" TextAlignment="Center" IsReadOnly="True"/>
    <TextBlock Grid.Row="1" Grid.Column="1"  Foreground="#5a5a5a"  TextAlignment="Center" Margin="1">Descuentos</TextBlock>
    <TextBox Grid.Row="1" Grid.Column="2" Margin="5" Text="0.0" TextAlignment="Center"/>

无论我如何调整它的大小,在我的机器上都可以正常工作。

如果您需要保留视图框,您可以使用绑定强制文本框大小相同

<Viewbox Grid.Row="0" Grid.Column="1" Stretch="Uniform" >
    <TextBlock Foreground="#5a5a5a" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"  TextAlignment="Center" Margin="1">Subtotal</TextBlock>
    </Viewbox>
    <Viewbox Grid.Row="0" Grid.Column="2" Stretch="Uniform" >
        <TextBox x:Name="SubtotalBox" Grid.Row="0" Grid.Column="2"  Margin="5" Text="155.60" TextAlignment="Center" IsReadOnly="True"/>
    </Viewbox>
    <Viewbox Grid.Row="1" Grid.Column="1" Stretch="Uniform" >
    <TextBlock Grid.Row="1" Grid.Column="1"  Foreground="#5a5a5a"  TextAlignment="Center" Margin="1">Descuentos</TextBlock>
    </Viewbox>
    <Viewbox Grid.Row="1" Grid.Column="2" Stretch="Uniform" >
            <TextBox Grid.Row="1" Grid.Column="2" Margin="5" Text="0.0" MaxLength="6" Height="{Binding ElementName=SubtotalBox, Path=ActualHeight}" Width="{Binding ElementName=SubtotalBox, Path=ActualWidth}" TextAlignment="Center"/>
    </Viewbox>

这会将第二个文本框的宽度和高度绑定到与第一个文本框相同的宽度和高度,保持一致并强制视图框调整大小以容纳更大的文本框。

© www.soinside.com 2019 - 2024. All rights reserved.