Reputation: 3713
I'm using wpf on C# to design GUI, and I want to get screen size (The value of Width and Height) from xaml code.
I knew how to get them from C# code as
Width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
Height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
But I don't know how to get them from XAML
code.
Upvotes: 14
Views: 23562
Reputation: 629
Say your parent container in the XAML is grid, name it as grdForm
In the code behind can get the dimensions as
double width=grdForm.ActualWidth
double height=grdForm.ActualHeight
Upvotes: 2
Reputation: 89745
This will work. You can read more here about SystemParameters
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<TextBlock Text="{Binding Source={x:Static SystemParameters.FullPrimaryScreenHeight}}" />
<TextBlock Text="{Binding Source={x:Static SystemParameters.FullPrimaryScreenWidth}}" />
<TextBlock Text="{Binding Source={x:Static SystemParameters.PrimaryScreenHeight}}" />
<TextBlock Text="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}}" />
</StackPanel>
</Window>
Upvotes: 24
Reputation: 427
Check out the SystemParameters class: http://msdn.microsoft.com/de-de/library/system.windows.systemparameters.fullprimaryscreenheight.aspx
You can use it like this:
<Object Attribute="{x:Static SystemParameters.FullPrimaryScreenHeight}"/>
(You could also use the x:Static notation to query the Windows Forms properties, however if you're developing a WPF application the SystemParameters class might be the way to go)
Upvotes: 10