Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different size form WPF - Design mode vs Runtime mode

I'm sorry for the beginner's question but 'im quite newbie in creating WPF forms. I've some problems to set and viewing correct size of forms in Visual Studio. In design mode the size is larger than in runtime mode. Here the XAML. Any ideas???

<Window
    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:EnginedrawWPF"
    xmlns:h="http://helix-toolkit.org/wpf" x:Class="MainWindow"
    mc:Ignorable="d"
    Title="Motore di disegno" Height="400.0" Width="970.0" ResizeMode="NoResize">
<Grid Margin="0,0,0,0">

    <h:HelixViewport3D x:Name="helixviewport3dobj"  Height="301" Margin="207,10,10.2,0" h:VerticalAlignment="Top" Foreground="Black" Background="#FFC0E2B5" >

        <h:HelixViewport3D.DefaultCamera>
            <PerspectiveCamera Position="0,-3.5,3.5" LookDirection="0,3.5,-3.5" UpDirection="0,0,1" FieldOfView="61" NearPlaneDistance="0.001"/>
        </h:HelixViewport3D.DefaultCamera>
        <h:SunLight />
        <h:GridLinesVisual3D Fill="#FFB9B9B9" Thickness="0.01" MinorDistance="0.2" MajorDistance="1"/>
    </h:HelixViewport3D>
    <Button x:Name="CBlineare" Content="Nuovo tratto lineare" HorizontalAlignment="Left" Height="30" Margin="10,10,0,0" VerticalAlignment="Top" Width="181"/>
    <Button x:Name="CBcurva90" Content="Aggiungi curva 90°" HorizontalAlignment="Left" Height="30" Margin="10,57,0,0" VerticalAlignment="Top" Width="181"/>
    <Button x:Name="CBcurva45" Content="Aggiungi curva 45°" HorizontalAlignment="Left" Height="30" Margin="10,105,0,0" VerticalAlignment="Top" Width="181"/>
</Grid> 
</Window>

screenshot

like image 378
Miller Tartari Avatar asked Sep 26 '22 17:09

Miller Tartari


1 Answers

You may put Grid tag inside the original Grid tag, with Height and Width set manually to fit into the design size. Then you put the SizeToContent="WidthAndHeight" to the Window tag.

Example of MainWindow.xaml (Visual Studio 2017 Community + WPF):

<Window x:Class="_171207_t0936_controlLocation.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:_171207_t0936_controlLocation"
        mc:Ignorable="d"
        Title="MainWindow" Height="378.431" Width="607.353"
        SizeToContent="WidthAndHeight">
    <Grid>
        <!-- In order to avoid the difference in size of debug and run modes -->
        <!-- set "SizeToContent=WidthAndHeight" -->
        <!-- Then put Grid insdie the original Grid with Height and Width set manually -->
        <!-- to fit into the design size -->
        <Grid>
            <Button Content="Button" HorizontalAlignment="Left" 
                    Margin="430,10,0,0" VerticalAlignment="Top" Width="75"/>
        </Grid>
    </Grid>
</Window>
like image 179
sevenOfNine Avatar answered Sep 30 '22 05:09

sevenOfNine