TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Devi
NA
55
16.3k
WPF center screen issues
Oct 29 2016 3:15 AM
I have my WPF xaml as follows
<Window
xmlns=
"http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x=
"http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d=
"http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc=
"http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local=
"clr-namespace:WPFApp1"
xmlns:Usercontrols=
"clr-namespace:WPFApp1.Usercontrols"
x:Class=
"WPFApp1.Window1"
mc:Ignorable=
"d"
Title=
"Window1"
Width=
"763"
Height=
"950"
ResizeMode=
"NoResize"
Loaded=
"Window_Loaded"
>
<Grid Height=
"224"
Width=
"Auto"
>
<CheckBox x:Name=
"Checkbox1"
Width=
"100px"
Height=
"25px"
Content=
"Checkbox"
Margin=
"90,-120,567,319"
/>
<CheckBox x:Name=
"Checkbox2"
Height=
"25px"
Content=
"Checkbox1"
Margin=
"178,-120,457,319"
/>
<CheckBox x:Name=
"Checkbox3"
Width=
"Auto"
Content=
"Checkbox2"
Margin=
"296,-120,309,319"
/>
<CheckBox x:Name=
"Checkbox4"
Width=
"Auto"
Content=
"Checkbox3"
Margin=
"420,-120,213,319"
/>
<GroupBox Name=
"grp1"
Header=
"Group1"
HorizontalAlignment=
"Left"
VerticalAlignment=
"Center"
Margin=
"92,56,0,98"
Width=
"590"
Height=
"70"
IsEnabled=
"False"
>
</GroupBox>
<GroupBox Name=
"grp2"
Header=
"Group2"
HorizontalAlignment=
"Left"
VerticalAlignment=
"Center"
Margin=
"92,136,0,18"
Width=
"590"
Height=
"70"
IsEnabled=
"False"
>
</GroupBox>
</Grid>
</Window>
I have written as follows to center the screen
double
width = window.ActualWidth;
double
height = window.ActualHeight;
if
(m_firstTime)
// First time ActualWidth and ActualHeight is not set
{
if
(!Double.IsNaN(window.Width)) { width = window.Width; }
if
(!Double.IsNaN(window.Height)) { height = window.Height; }
m_firstTime =
false
;
}
// Set Left and Top manually and calculate center of screen.
window.Left = (SystemParameters.WorkArea.Width - width) / 2
+ SystemParameters.WorkArea.Left;
window.Top = (SystemParameters.WorkArea.Height - height) / 2
+ SystemParameters.WorkArea.Top;
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
Reply
Answers (
6
)
How to bind combobox in WPF in C#.Net?
WPF - Operation is not valid while ItemsSource is in use.