2012-06-06 34 views
1

MainWindow.XAML:應用LayoutTransform只有父母而不是任何特定的孩子

<Window x:Class="TestWPFApp.MainWindow" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      Name="myMainWindow" 
      Title="MainWindow" 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" Name="myButton"/> 
      </Grid> 
     </Grid> 

    </Window> 

主窗口代碼隱藏:

public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     #region ScaleValue Depdency 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; 
     } 

     protected virtual void OnScaleValueChanged(double oldValue, double newValue) 
     { 

     } 

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

     private void MainGrid_SizeChanged(object sender, EventArgs e) 
     { 
      CalculateScale(); 
     } 

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

我有這個示例應用程序。我正在MainGrid上應用LayoutTransform以通過調整窗口大小來縮放應用程序。我如何避免我的控制myButton應用該轉換?我不希望這種轉換適用於它。

+0

看看[ViewBox](http://msdn.microsoft.com/en-us/library/system.windows.controls.viewbox.aspx)你在這裏創建的東西看起來像ViewBox已經做了什麼,但在WPF內建。 – dowhilefor

+0

沒有ViewBox不會像我想要的那樣擴展它。這個解決方案完全符合我的要求。 –

回答

8

兩個選項

1)不要讓一個孩子,而不是隻讓它在視覺上重疊,例如使用第二個網格。

2)將轉換的逆應用於兒童,這將否定轉換,可能是昂貴的,但應該做的伎倆。

<Button LayoutTransform="{Binding ElementName=MainGrid, Path=LayoutTransform.Inverse}"/> 

我個人會使用第一個選項。

+0

由於某些原因,我必須讓它成爲孩子。如何將逆變換應用於孩子? –

+0

@BilalHashmi我更新了我的答案。 – dowhilefor

+0

好的。讓我試一試 –

相關問題