2013-09-16 54 views
0

我試圖動態擴展我的我的基於WPF應用程序的UI的寬度,我想使用當前的分辨率和我的原始分辨率採用這樣的比例:數據綁定到表單

<Grid.LayoutTransform> 
     <ScaleTransform CenterX="0" CenterY="0" ScaleX="{Binding FormWidth/NativeREsolution}" ScaleY="{Binding FormWidth/NativeREsolution}"/> 
    </Grid.LayoutTransform> 

我發現縮放變換的原因是縮放容器內的所有UI元素,包括框架和子頁面。

有沒有辦法做到這一點?

另外還有更好的方法來動態縮放應用程序取決於窗口的大小?

+0

什麼?這是WPF嗎?你並不需要那個。在這個問題中沒有足夠的信息來提供答案。請說明 –

+0

是的,它是WPF,添加了標籤並希望添加足夠的。我需要的是在調整它的大小時動態調整我的應用程序的大小。 – Herrozerro

+0

我不明白。 WPF本質上是獨立於分辨率的。你爲什麼要申請一個'ScaleTransform'? –

回答

2

WPF is Resolution Independent by nature.

<Window ..> 
    <Grid> 

    <!-- Content here --> 

    </Grid> 
</Window> 

上述XAML將使Grid可拉伸至窗口大小。沒有可怕的WinForms - 像黑客所需。

編輯:

如果你想Everything(包括字體大小)爲Window內部擴展,只需使用視框:

<Window> 
    <Viewbox> 
     <Grid> 

     <!-- Content here --> 

     </Grid> 
    </Viewbox> 
</Window> 
+0

內部的所有元素呢?他們會擴展嗎?字體會縮放嗎?我發現ScaleTransform運行良好的主要原因是因爲我有幾個框架內有不同的頁面。 – Herrozerro

+0

@Herrozerro看到我的編輯 –

+0

是固定的。哇。我無法相信我忽視了將整個網格放在視圖框中。 – Herrozerro

0

最簡單的辦法是使用視框,但它也可以很簡單地計算Window調整大小的比例。

視框

<Window x:Class="MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Viewbox Stretch="Uniform"> 
    <Grid> 
     <Label> 
      Hello world 
     </Label> 
    </Grid> 
</Viewbox> 
</Window> 

手動縮放

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" 
    Name="myMainWindow" 
    Width="200" Height="250"> 
<Grid Name="MainGrid" SizeChanged="MainGrid_SizeChanged"> 
    <Grid.LayoutTransform> 
     <ScaleTransform x:Name="ApplicationScaleTransform" 
         CenterX="0" 
         CenterY="0" 
         ScaleX="{Binding ElementName=myMainWindow, Path=ScaleValue}" 
         ScaleY="{Binding ElementName=myMainWindow, Path=ScaleValue}" /> 
    </Grid.LayoutTransform> 
    <Grid VerticalAlignment="Center" HorizontalAlignment="Center" Height="150"> 
     <TextBlock FontSize="20" Text="Hello World" Margin="5" VerticalAlignment="Top" HorizontalAlignment="Center"/> 
     <Button Content="Button" VerticalAlignment="Bottom" HorizontalAlignment="Center"/> 
    </Grid> 
</Grid> 

其中網格是使用通過參考是一個編碼ScaleValue依賴屬性確定的ScaleX & scaleY屬性一個LayoutTransform縮放在Window_Resize事件期間使用類似於

的代碼計算
#region ScaleValue Dependency Property 

    public static readonly DependencyProperty ScaleValueProperty = DependencyProperty.Register("ScaleValue", typeof(double), typeof(MainWindow), new UIPropertyMetadata(1.0, new PropertyChangedCallback(OnScaleValueChanged), new CoerceValueCallback(OnCoerceScaleValue))); 

    private static object OnCoerceScaleValue(DependencyObject o, object value) 
    { 
     MainWindow mainWindow = o as MainWindow; 
     if (mainWindow != null) 
      return mainWindow.OnCoerceScaleValue((double)value); 
     else 
      return value; 
    } 

    private static void OnScaleValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) 
    { 
     MainWindow mainWindow = o as MainWindow; 
     if (mainWindow != null) 
      mainWindow.OnScaleValueChanged((double)e.OldValue, (double)e.NewValue); 
    } 

    protected virtual double OnCoerceScaleValue(double value) 
    { 
     if (double.IsNaN(value)) 
      return 1.0f; 

     value = Math.Max(0.1, value); 
     return value; 
    } 

    public double ScaleValue 
    {    
     get 
     { 
      return (double)GetValue(ScaleValueProperty); 
     } 
     set 
     { 
      SetValue(ScaleValueProperty, value); 
     } 
    } 
    #endregion 


    private void CalculateScale() 
     { 
      double yScale = ActualHeight/250f; 
      double xScale = ActualWidth/200f; 
      double value = Math.Min(xScale, yScale); 
      ScaleValue = (double)OnCoerceScaleValue(MainGrid, value); 
     } 
+0

如果我沒有記錯,那麼viewbox的問題就是一個viewbox只能容納一個元素。雖然這可能現在工作,因爲我們正在使用框架來保存我們的網頁。 – Herrozerro

+0

@Herrozerro如果你是「顯示多個頁面」(不管是什麼意思),只需在視圖框中放置一個ContentPresenter並綁定Content屬性即可。我不明白你想在程序代碼中做什麼是最重要的。 –