Devi

Devi

  • 1.9k
  • 55
  • 11.2k

WPF center screen issues

Oct 29 2016 3:15 AM
I have my WPF xaml as follows

  1. <Window  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6.         xmlns:local="clr-namespace:WPFApp1"  
  7.         xmlns:Usercontrols="clr-namespace:WPFApp1.Usercontrols" x:Class="WPFApp1.Window1"  
  8.         mc:Ignorable="d"  
  9.         Title="Window1" Width="763" Height="950" ResizeMode="NoResize" Loaded="Window_Loaded">  
  10.     <Grid Height="224" Width="Auto">  
  11.           
  12.         <CheckBox x:Name="Checkbox1" Width="100px" Height="25px" Content="Checkbox" Margin="90,-120,567,319"/>  
  13.         <CheckBox x:Name="Checkbox2" Height="25px" Content="Checkbox1"  Margin="178,-120,457,319"/>  
  14.         <CheckBox x:Name="Checkbox3" Width="Auto" Content="Checkbox2"  Margin="296,-120,309,319"/>  
  15.         <CheckBox x:Name="Checkbox4" Width="Auto" Content="Checkbox3"  Margin="420,-120,213,319"/>  
  16.           
  17.         <GroupBox Name="grp1" Header="Group1" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="92,56,0,98" Width="590" Height="70" IsEnabled="False">  
  18.               
  19.         </GroupBox>  
  20.         <GroupBox Name="grp2" Header="Group2" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="92,136,0,18" Width="590" Height="70" IsEnabled="False">  
  21.               
  22.         </GroupBox>  
  23.           
  24.     </Grid>  
  25. </Window>  
  26.   
 I have written as follows to center the screen
  1. double width = window.ActualWidth;  
  2.             double height = window.ActualHeight;  
  3.    
  4.             if (m_firstTime) // First time ActualWidth and ActualHeight is not set  
  5.             {  
  6.                 if (!Double.IsNaN(window.Width)) { width = window.Width; }  
  7.                 if (!Double.IsNaN(window.Height)) { height = window.Height; }  
  8.    
  9.                 m_firstTime = false;  
  10.             }  
  11.    
  12.             // Set Left and Top manually and calculate center of screen.  
  13.             window.Left = (SystemParameters.WorkArea.Width - width) / 2   
  14.                 + SystemParameters.WorkArea.Left;  
  15.             window.Top = (SystemParameters.WorkArea.Height - height) / 2  
  16.                 + SystemParameters.WorkArea.Top;  
  17.   
 But in my laptop it is going to the top so that few controls not getting visible so can some one help me to center on any screen resolution

Answers (6)