缩放,同时保持水平对齐

时间:2015-08-04 13:33:01

标签: c# .net wpf xaml

我有WPF控件:

<UserControl x:Class="MyProject.LabelWithUnit"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="30" d:DesignWidth="150">
       <Viewbox StretchDirection="Both" Stretch="Uniform">
            <Grid Width="150">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Label x:Name="ValueLabel" Grid.Column="0" Content="1013.0" Margin="0" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" FontSize="18"/>
                <Label x:Name="UnitLabel" Grid.Column="1" Content="m/s" Margin="1" FontSize="10" />
            </Grid>
        </Viewbox>
</UserControl>

现在的表现如何:

默认值:

enter image description here

宽度增加时: (这是我需要改变的) enter image description here

当我增加身高时它会如何缩放:(应保持原样) enter image description here

表现如何:

enter image description here

(只有这种行为应该改变 - “m / s”应该贴在右上角): enter image description here

enter image description here

所以,“m / s”部分应始终贴在右上角,数字部分应保持在中间位置附近。当我增加Height我的控件时,它应该缩放Label s。

编辑:添加了更多图片。

5 个答案:

答案 0 :(得分:0)

更改ViewBox控件上的拉伸:

<Viewbox StretchDirection="UpOnly" Stretch="Uniform">

答案 1 :(得分:0)

尝试从网格中删除宽度可以解决您的问题。

location.search

修改

<Grid Width="150"> 

修改

如下所示,

<Grid >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="25" />
        </Grid.ColumnDefinitions>
        <Label x:Name="ValueLabel" Grid.Column="0" Content="1013.0" Margin="0" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" FontSize="18"/>
        <Label x:Name="UnitLabel" Grid.Column="1" Content="m/s" FontSize="10" />
    </Grid>

答案 2 :(得分:0)

你试过吗

<Label x:Name="UnitLabel" Grid.Column="1" Content="m/s" FontSize="10" HorizontalAlignment="Right" VerticalAlignment="Top" />

编辑:我也不确定这些专栏如何帮助你

答案 3 :(得分:0)

我得到你想要的样式,只需将VerticalAlignment of Labels交换为Stretch

<UserControl x:Class="MyProject.LabelWithUnit"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="30" d:DesignWidth="150">
       <Viewbox StretchDirection="Both" Stretch="Uniform">
            <Grid Width="1900">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Label x:Name="ValueLabel" Grid.Column="0" Content="1013.0" Margin="0" VerticalAlignment="Stretch" HorizontalAlignment="Center" FontWeight="Bold" FontSize="18"/>
                <Label x:Name="UnitLabel" Grid.Column="1" Content="m/s" Margin="1" FontSize="10" VerticalAlignment="Stretch"/>
            </Grid>
        </Viewbox>
</UserControl>

答案 4 :(得分:0)

    <Grid>
        <Viewbox Margin="0">
            <Label Margin="0" Padding="1" FontWeight="Bold"/>
        </Viewbox>
        <Grid SnapsToDevicePixels="True">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="0.8*" />
            </Grid.RowDefinitions>
            <Viewbox HorizontalAlignment="Right" Stretch="Uniform" Margin="3,0">
                <Label Margin="0" Padding="0"/>
            </Viewbox>
        </Grid>
    </Grid>