2017-05-24 144 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

我希望,通過將相同的邏輯在上下文將你就能瞭解你的情況的確切情況,您可以選擇最好的解決方案爲您的需求。