2016-10-12 7 views
0

子ウィンドウのサイズを手動で変更する必要があります。 これを行うためには、私は親ウィンドウに次のコードを作っ:行WPF:子ウィンドウのサイズをプログラムで設定する

w.Width *= m_RatioX/w.m_RatioX; 
w.Height *= m_RatioY/w.m_RatioY; 

SizeChanged += (sender, e) => 
{ 
    if (e.PreviousSize.Width != 0 && e.PreviousSize.Height != 0) 
    { 
     m_RatioX *= e.NewSize.Width/e.PreviousSize.Width; 
     m_RatioY *= e.NewSize.Height/e.PreviousSize.Height; 
    } 
    WPFWindow p = ((WPFWindow)sender); 
    foreach (WPFWindow w in p.OwnedWindows) 
    { 
     if (w.m_doStretch) 
     { 
      // this work fine 
      w.Left *= m_RatioX/w.m_RatioX; 
      w.Top *= m_RatioY/w.m_RatioY; 
      w.Canvas.RenderTransform = new ScaleTransform(m_RatioX, m_RatioY); 

      // but not for the size modification 
      w.Width *= m_RatioX/w.m_RatioX; 
      w.Height *= m_RatioY/w.m_RatioY; 
      w.m_RatioX = m_RatioX; 
      w.m_RatioY = m_RatioY; 
     } 
    } 
}; 

を、彼らは子ウィンドウの大きさに全く影響を与えないという意味で動作しません。 。

いくつかの調査の後、次の2つの選択肢も試しましたが、最初のものは、2番目のクラッシュのあるウィンドウサイズには影響しません。NullReferenceException

// first alternative 
Window.GetWindow(w).Width = Window.GetWindow(w).Width * m_RatioX/w.m_RatioX; 
Window.GetWindow(w).Height = Window.GetWindow(w).Height * m_RatioY/w.m_RatioY; 

// second one 
Application.Current.MainWindow = w; 
Application.Current.MainWindow.Width = Application.Current.MainWindow.Width * m_RatioX/w.m_RatioX; 
Application.Current.MainWindow.Height = Application.Current.MainWindow.Height * m_RatioY/w.m_RatioY; 

したがって、レンダリングが効果的にサイズ変更されるように子ウィンドウのサイズを変更する必要がありますか?

EDIT:

いくつかの精度:

  • SizeChangedハンドラは、親ウィンドウに呼ばれています。
  • m_RatioXm_RatioYデフォルト値は[OK]を、私は解決策を見つけた1
+0

元のコード(2つの選択肢なし)を確認したところ、正常に動作しているようです。あなたがチェックする必要がある2つの事柄:1.) 'm_RatioX'と' m_RatioY'の値が決して '0d'でない場合にのみ動作します。あなたのケースの初期値は何ですか? 2)親ウィンドウまたは子ウィンドウでのみ 'SizeChanged'を購読していますか?子ウィンドウで 'SizeChanged'を処理している場合、エフェクトは奇妙な光学的方法でカスケードします。 – haindl

+0

もう1つの観察:2番目の選択肢で 'NullReferenceException'の原因は何ですか? 'w'がnullの場合は、より一般的な問題があります。 – haindl

+0

いくつかの点を明確にするために最初のメッセージでいくつか編集しました。また、 'NullReferenceException'は' Application.Current.MainWIndow'に 'w'に影響を与えようとしたときに発生しますが、' w'は 'null'ではありません。 –

答えて

0

です。 Width/Heightプロパティを直接使用する代わりに、関連するDependencyPropertyを使用する必要があります。 、すなわちw.Width = <value>の代わりにw.SetValue(Window.WidthProperty, <value>);を使用してください。

関連する問題