2009-06-05 8 views
17

WinForms에서 Form은 클라이언트 영역의 크기, 즉 제목 표시 줄 및 창 테두리 안의 영역을 반환하는 ClientSize 속성 (Control에서 상 속됨)을가집니다.WPF 윈도우의 ClientSize를 얻는 방법?

WPF에서 비슷한 것을 볼 수 없습니다. ClientSize, ClientWidth, ClientHeight, GetClientSize() 또는 이름을 추측 할 수있는 다른 어떤 것도 없습니다.

WPF 창의 클라이언트 크기를 가져 오는 방법은 무엇입니까?

답변

11

할 수있는 한 가지 방법은 최상위 자식 요소 인 this.Content을 해당 유형으로 캐스팅하고 그 위에 .RenderSize을 호출하여 크기를 지정하는 것입니다.

<Window x:Class="XML_Reader.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="400" Width="600" WindowStyle="SingleBorderWindow"> 
    <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch"> 
    </Grid> 
</Window> 

((Grid)this.Content).RenderSize.Height 
((Grid)this.Content).RenderSize.Width 

편집 :

트렌트가 말했듯이, ActualWidthActualHeight도 가능한 솔루션입니다. 기본적으로 내가 위에 뒀던 것을 얻는 더 쉬운 방법.

2

한 가지 방법은 아래 코드를 사용하는 것입니다. XAML :

<Window x:Class="WpfApplication1.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:WpfApplication1" 
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded"> 
    <Canvas> 
    </Canvas> 
</Window> 

C 번호 : 나를 위해 0.0에 나오는

using System.Windows; 

using System.IO; 
using System.Xml; 
using System.Windows.Controls; 

namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for Window1.xaml 
    /// </summary> 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 
     } 

     private void Window_Loaded(object sender, RoutedEventArgs e) 
     { 
      double dWidth = -1; 
      double dHeight = -1; 
      FrameworkElement pnlClient = this.Content as FrameworkElement; 
      if (pnlClient != null) 
      { 
       dWidth = pnlClient.ActualWidth; 
       dHeight = pnlClient.ActualHeight; 
      } 
     } 
    } 
} 
10
var h = ((Panel)Application.Current.MainWindow.Content).ActualHeight; 
var w = ((Panel)Application.Current.MainWindow.Content).ActualWidth; 
+0

는 –

+1

@WelshKing -로드 된 이벤트 해고 했습니까? 윈도우가 렌더링 될 때까지는 크기가 없습니다. – Tristan

관련 문제