0

I have a question related WPF MVVM- I want that MainWindow height and width will be set according current view height\width.

How can I define it?

MainWindow.xaml

    <Window x:Class="Project.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"
        xmlns:local="clr-namespace:Project"
        xmlns:views ="clr-namespace:Project.Views"
        mc:Ignorable="d"
        Title="MainWindow" 
        Height="{Binding Source = {x:Static SystemParameters.PrimaryScreenHeight}, Converter={local:RatioConverter}, ConverterParameter='0.6'}"  
        Width="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={local:RatioConverter}, ConverterParameter='0.5'}"
        >
    <Grid>
        <ContentControl Name="placeholder"></ContentControl>
    </Grid>
</Window>

In One of the views

 <UserControl x:Class="Project.Views.MsgBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Project.Views"
             mc:Ignorable="d" 
>
    <UserControl.Background>
        <SolidColorBrush Color="#000000" Opacity="0.5"  />
    </UserControl.Background>
    <Grid Opacity="1" >
        <Grid Width="360"   Background="White" Opacity="1" Margin="0,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition MinHeight="90" Height="auto"/>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

    ... any code...

    </Grid>
</UserControl>
Programmer
  • 371
  • 1
  • 8
  • 21

1 Answers1

0

If you want Window to have the same size as its content, set SizeToContent property to WidthAndHeight:

<Window  ...  SizeToContent="WidthAndHeight">
    <Grid>
        <ContentControl Name="placeholder"></ContentControl>
    </Grid>
</Window>
Sam
  • 1,384
  • 2
  • 20
  • 30