How to obtain screen size from xaml?

前端 未结 3 725
独厮守ぢ
独厮守ぢ 2020-12-19 03:14

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



        
相关标签:
3条回答
  • 2020-12-19 03:46

    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>
    
    0 讨论(0)
  • 2020-12-19 03:56

    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)

    0 讨论(0)
  • 2020-12-19 03:56

    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

    0 讨论(0)
提交回复
热议问题