简体   繁体   English

MainWindow的高度\\宽度将根据当前视图WPF进行设置

[英]MainWindow height\width will be set according current view WPF

I have a question related WPF MVVM- I want that MainWindow height and width will be set according current view height\\width. 我有一个与WPF MVVM有关的问题,我希望MainWindow的高度和宽度将根据当前视图的height \\ width进行设置。

How can I define it? 我该如何定义?

MainWindow.xaml 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>

If you want Window to have the same size as its content, set SizeToContent property to WidthAndHeight : 如果希望Window与它的内容具有相同的大小,请将SizeToContent属性设置为WidthAndHeight

<Window  ...  SizeToContent="WidthAndHeight">
    <Grid>
        <ContentControl Name="placeholder"></ContentControl>
    </Grid>
</Window>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM