Viewbox填充所有列网格空间WPF

时间:2015-10-28 02:07:34

标签: c# wpf xaml viewbox

我正在创建一个程序,告诉用户他的帐户余额和类似的东西。我在带有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>

结果如下:

enter image description here

正如你可以看到&#34;折扣&#34; TextBox比上面小,我需要它们具有相同的宽度和高度。我怎样才能实现这一目标?我把所有内容放在ViewBox中为我调整大小,但是它是对的吗?我已经尝试了几种类似this的方法,但它使文本非常小。

1 个答案:

答案 0 :(得分:1)

根本不需要Viewbox:

    <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,可以使用Bindings强制您的文本框大小相同

<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>

这会将第二个文本框的宽度和高度与第一个文本框的宽度和高度绑定,使其保持一致,并强制Viewbox调整大小以容纳更大的TextBox。