Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set WPF window size is 25 percent of relative monitor screen

I have a WPF window and How can I set WPF window size is 25 percent of relative monitor screen.How can I set these properties.

like image 946
user3188127 Avatar asked Aug 21 '14 12:08

user3188127


2 Answers

In your MainWindow Constructor add

this.Height = (System.Windows.SystemParameters.PrimaryScreenHeight * 0.25);
this.Width = (System.Windows.SystemParameters.PrimaryScreenWidth * 0.25);

Also don't set WindowState="Maximized" in your MainWindows.xaml otherwise it won't work. Hope this helps.

like image 80
Vyas Avatar answered Nov 14 '22 20:11

Vyas


Or using xaml bindings

MainWindow.xaml

<Window x:Class="App.MainWindow"
        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"
        mc:Ignorable="d"
        Title="Main Window" 
        Height="{Binding Source={x:Static SystemParameters.PrimaryScreenHeight}, Converter={StaticResource WindowSizeConverter}, ConverterParameter='0.6'}" 
        Width="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={StaticResource WindowSizeConverter}, ConverterParameter='0.8'}" >

Resources.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:App">
    <local:WindowSizeConverter x:Key="WindowSizeConverter"/>
</ResourceDictionary>

WindowSizeConverter.cs

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace App
{
    public class WindowSizeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double size = System.Convert.ToDouble(value) * System.Convert.ToDouble(parameter, CultureInfo.InvariantCulture);
            return size.ToString("G0", CultureInfo.InvariantCulture);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => DependencyProperty.UnsetValue;
    }
}
like image 23
Arsinclair Avatar answered Nov 14 '22 21:11

Arsinclair