2017-05-24 141 views
0

一直在努力与此一段时间了。我仍然不知道发生了什么事。 Viewbox Scale =“UniformToFill”应该使内容伸展直到它填满屏幕。在下面的图片中。我使用相同的控件。但我得到不同的结果。我真正想要的就是看起来像img1。当我说我看起来我的意思是我希望既能缩放以适应屏幕,又不会像img1那样跳出界限。 img1 - correct oneWPF - 缩放uniformtofill不按预期工作

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> 

回答

0

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

您可能需要使用,如果你想舒展所有输入的内容全部输出区域填写,但它会使用不同大小的比率水平和垂直刻度。

另一种选择是使用均匀如果您想使用相同的比例缩放并确保所有输入显示在输出区域中,但某些边距在输出的左右或上下两侧仍可能为空。

我这个XAML例如测试(根据您的问题):

<Viewbox Stretch="UniformToFill"> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="auto"/> 
       <RowDefinition Height="auto"/> 
      </Grid.RowDefinitions> 
      <Label x:Name="SportLabel" Content="Test" Foreground="Orange"/> 
      <ScrollViewer x:Name="SV1" Grid.Row="1" ScrollViewer.VerticalScrollBarVisibility="Hidden"> 
       <DataGrid Name="dataGrid"> 
        <DataGrid.Columns> 
         <DataGridTextColumn Binding="{Binding}"/> 
        </DataGrid.Columns> 
        <DataGrid.Items> 
         <sys:String>Test1</sys:String> 
         <sys:String>Test2</sys:String> 
         <sys:String>Test3</sys:String> 
         <sys:String>Test4</sys:String> 
        </DataGrid.Items> 
       </DataGrid> 
      </ScrollViewer> 
     </Grid> 
    </Viewbox> 

,并检查结果在四个方面:

  1. 如果容器的宽度足够大,以UniformToFill
  2. 如果容器的宽度较小,带UniformToFill
  3. 如果我们使用填充代替UniformToFill
  4. 如果我们使用统一的,而不是UniformToFill

Test screenshots

我希望,通过将相同的逻辑在上下文将你就能了解你的情况的确切情况,您可以选择最好的解决方案为您的需求。