WPF - 缩放uniformtofill无法按预期工作

时间:2017-05-24 00:32:49

标签: wpf xaml

现在已经挣扎了一段时间。我仍然没有真正得到正在发生的事情。 Viewbox Scale =" UniformToFill"应该让内容延伸到填满屏幕。在以下图片中。我使用相同的控件。但我得到了不同的结果。我真正想要的是看起来像img1。当我说看起来我的意思是我想要缩放以适应屏幕而不是像img1那样超出界限。 img1 - correct one

img2 - wrong scaling

 <UserControl x:Class="lou"
         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"             
         xmlns:local="clr-namespace:lou.Controls"
         xmlns:src="clr-namespace:lou"
         mc:Ignorable="d" 
         x:Name="control" DataContext="{Binding RelativeSource={RelativeSource Self}}" MaxHeight="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=ActualHeight}">
<Viewbox Stretch="UniformToFill">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <Label x:Name="SportLabel" Content="{Binding Path=SportName}" Foreground="Orange"/>
        <ScrollViewer x:Name="SV1" Grid.Row="1" ScrollViewer.VerticalScrollBarVisibility="Hidden" >
            <DataGrid Name="dataGrid" ItemsSource="{Binding DataTable}" Style="{StaticResource dataGrid}" ColumnHeaderStyle="{StaticResource DataGridColumnStyle}"/>
        </ScrollViewer>
    </Grid>
</Viewbox>

  <Window x:Class="lou.test"
    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"
    xmlns:local="clr-namespace:lou"
    mc:Ignorable="d"
    Title="asdfasdf" Height="300" Width="300"
    WindowStyle="None"
    WindowState="Normal"
    x:Name="this" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid x:Name="grid1" Background="Black">
    <ScrollViewer x:Name="mainWindowScroller" VerticalScrollBarVisibility="Visible" CanContentScroll="True">
        <ItemsControl x:Name="itemsControl" ItemsSource="{Binding Collection}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate x:Name="itemsPanelTemplate">
                    <StackPanel x:Name="stackPanel" Orientation="Vertical" IsItemsHost="True" 
                                MouseLeftButtonDown="stackPanel_MouseLeftButtonDown" ScrollViewer.VerticalScrollBarVisibility="Hidden"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </ScrollViewer>
</Grid>

1 个答案:

答案 0 :(得分:0)

UniformToFill将使用相同的水平和垂直缩放比率将输入内容拉伸到输出区域,并将左上角视为变换的原点。因此,整个宽度或整个高度都与输出区域宽度或高度相匹配(取决于输入和输出的大小比率)。

如果要将所有输入内容拉伸到所有输出区域,可能需要使用“填充”,但它会对水平和垂直比例使用不同的大小比例。

如果您想使用相同的比例缩放并确保所有输入都显示在输出区域中,则另一个选项是使用Uniform,但是在输出的左右或上下两侧可能会保留一些边距。 / p>

我测试了这个XAML示例(基于您的问题):

public class News
{
    public Guid ID {get;set;}
    public string Details {get;set;}
}

并以四种方式检查结果:

  1. 如果容器的宽度足够大,使用UniformToFill
  2. 如果容器的宽度较小,则使用UniformToFill
  3. 如果我们使用Fill而不是UniformToFill
  4. 如果我们使用Uniform而不是UniformToFill
  5. Test screenshots

    我希望通过在您的上下文中应用相同的逻辑,您将能够了解您的具体情况,并且您可以根据自己的需要选择最佳解决方案。