预览中的WPF控件在运行时不匹配尺寸和位置-如何修复?

8i9zcol2  于 2023-05-23  发布在  其他
关注(0)|答案(1)|浏览(283)

控件在我的WPF预览窗口中的尺寸和位置与运行时的实际窗口不匹配。我不知道它为什么这样做,如果它是有关的代码或我的屏幕。下面是代码:

<Window x:Name="HomeWindow" x:Class="Progetto_Amada.MainWindow" x:FieldModifier="public"
        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:MyNamespace="clr-namespace:Progetto_Amada"
        xmlns:local="clr-namespace:Progetto_Amada"
        mc:Ignorable="d"
        Title="Amada Reminder for General Administration" MinWidth="1100" MinHeight="650" Background="#FF323232" Loaded="HomeWindow_Loaded"  WindowState="Maximized" HorizontalAlignment="Left" SizeToContent="WidthAndHeight"
        >

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="4*" />
            <ColumnDefinition Width="4*" />
            <ColumnDefinition Width="4*" />
            <ColumnDefinition Width="4*" />
        </Grid.ColumnDefinitions>

        <Menu Name="TopMenu"
              Height="20"
              VerticalAlignment="Top"
              Background="#FF323232"
              Foreground="White"
              Grid.ColumnSpan="4">

            <MenuItem
                Header="File"
                FontFamily="Arial">

                <MenuItem
                    Name="AddFile"
                    Header="Allega un file"
                    Foreground="Black"
                    Click="CaricaFile_Click">

                    <MenuItem.Icon>
                        <Image Source="C:\Users\david\Desktop\progetto-pcto-requests\Progetto-Amada\bin\Debug\net6.0-windows\assets\file.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Name="RemoveFile"
                          Header="Rimuovi un file"
                          Foreground="Black"
                          Click="RimuoviFile_Click">

                    <MenuItem.Icon>
                        <Image Source="C:\Users\david\Desktop\progetto-pcto-requests\Progetto-Amada\bin\Debug\net6.0-windows\assets\file.png" />
                    </MenuItem.Icon>
                </MenuItem>
            </MenuItem>
            <MenuItem
                Header="Visualizza"
                FontFamily="Arial">

                <MenuItem
                    Name="VediContratti"
                    Header="Visualizza tutti i contratti"
                    Foreground="Black"
                    Click="TuttiIContratti_Click">

                    <MenuItem.Icon>
                        <Image Source="C:\Users\david\Desktop\progetto-pcto-requests\Progetto-Amada\bin\Debug\net6.0-windows\assets\agreement.png" />
                    </MenuItem.Icon>
                </MenuItem>

                <MenuItem
                    Name="VediMacchine"
                    Header="Visualizza tutte le macchine"
                    Foreground="Black"
                    Click="TutteLeMacchine_Click">

                    <MenuItem.Icon>
                        <Image Source="C:\Users\david\Desktop\progetto-pcto-requests\Progetto-Amada\bin\Debug\net6.0-windows\assets\machine.png" />
                    </MenuItem.Icon>
                </MenuItem>
            </MenuItem>

            <MenuItem
                Header="Utente"
                FontFamily="Arial">
                <MenuItem
                    Name="NomeUtente"
                    Header="Utente: [ Nome utente ]"
                    Foreground="Black"
                    IsEnabled="False">

                </MenuItem>
                <MenuItem
                    Name="InfoUtente"
                    Header="Informazioni sull'utente"
                    Foreground="Black">
                    <MenuItem.Icon>
                        <Image Source="C:\Users\david\Desktop\progetto-pcto-requests\Progetto-Amada\bin\Debug\net6.0-windows\assets\user.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Name="AccessoCambio" Header="Cambia utente" Foreground="Black" Click="CambiaUtente_Click">
                    <MenuItem.Icon>
                        <Image Source="C:\Users\david\Desktop\progetto-pcto-requests\Progetto-Amada\bin\Debug\net6.0-windows\assets\user.png" />
                    </MenuItem.Icon>
                </MenuItem>
            </MenuItem>

        </Menu>
        <Button
            Grid.Column="3"
            x:Name="btnCerca"
            Content="CERCA"
            HorizontalAlignment="Right"
            Margin="0,40,20,0"
            VerticalAlignment="Top"
            Height="36"
            Width="80"
            Click="btnCerca_Click"
            Grid.ColumnSpan="4"
            FontWeight="Bold"
            FontFamily="Arial"/>

        <Label
            Grid.Column="0"
            Content="Scadenze Imminenti:"
            HorizontalAlignment="Left"
            Margin="8,100,0,0"
            VerticalAlignment="Top"
            Foreground="White"
            FontSize="25"
        />
        <Label Grid.Column="1"
               Content="Scadenze Urgenti:"
               HorizontalAlignment="Left"
               Margin="8,100,0,0"
               VerticalAlignment="Top"
               Foreground="White"
               FontSize="25"
        />
        <Label Grid.Column="2"
               Content="Scadenze Remote:"
               HorizontalAlignment="Left"
               Margin="8,100,0,0"
               VerticalAlignment="Top"
               Foreground="White"
               FontSize="25"
        />
        <Label Grid.Column="3"
               Content="Tutte le scadenze:"
               HorizontalAlignment="Left"
               Margin="6,100,0,0"
               VerticalAlignment="Top"
               Foreground="White"
               FontSize="25"
        />

        <StatusBar
            Name="UsernameBar" 
            Height="35" 
            VerticalAlignment="Bottom"
            Background="Black"
            Foreground="White"
            Grid.ColumnSpan="4" Visibility="Visible" >

            <Label 
                Name="UtenteLabel" 
                Content="Utente: [ Nome Utente ]" 
                Foreground="White" 
                FontSize="15" 
                Width="296" 
                Height="30"
                HorizontalAlignment="Center"
                VerticalAlignment="Center" 
                Visibility="Visible" 
            />
        </StatusBar>

        <TextBox x:Name="ricercaTextbox" 
            TextWrapping="Wrap" 
            Height="36" 
            VerticalAlignment="Top" 
            Margin="0,40,105,0" 
            FontSize="20" 
            VerticalContentAlignment="Center"
            Grid.Column="2"
            HorizontalAlignment="Right"
            Width="273" Grid.ColumnSpan="2"
        />

        <Grid Grid.Column="0" Margin="10,148,10,40">
            <ScrollViewer>
                <StackPanel
                    Name="ScadenzeImminenti"
                    x:FieldModifier="public">
                </StackPanel>
            </ScrollViewer>
        </Grid>
        <Grid Grid.Column="1" Margin="10,148,10,40">
            <ScrollViewer>
                <StackPanel
                    Name="ScadenzeUrgenti"
                    x:FieldModifier="public">
                </StackPanel>
            </ScrollViewer>
        </Grid>
        <Grid Grid.Column="2" Margin="10,148,10,40">
            <ScrollViewer>
                <StackPanel
                    Name="ScadenzeRemote"
                    x:FieldModifier="public">
                </StackPanel>
            </ScrollViewer>
        </Grid>
        <Grid Grid.Column="3" Margin="10,148,10,40">
            <ScrollViewer>
                <StackPanel
                    Name="TutteLeScadenze"
                    x:FieldModifier="public">
                </StackPanel>
            </ScrollViewer>
        </Grid>

        <Button
            Grid.Column="0"
            x:Name="addScadenzaBtn"
            Content="AGGIUNGI UNA SCADENZA"
            HorizontalAlignment="Left"
            Margin="10,40,0,0"
            VerticalAlignment="Top"
            Height="36" Width="275"
            Click="addScadenzaBtn_Click"
            FontWeight="Bold"
            FontFamily="Arial"
            Grid.ColumnSpan="2"/>
        <Button
            x:Name="allMacchineBtn"
            Content="TUTTE LE MACCHINE"
            HorizontalAlignment="Left"
            Margin="335,40,0,0"
            VerticalAlignment="Top"
            Height="36"
            Width="310"
            FontWeight="Bold"
            FontFamily="Arial"
            Click="allMacchineBtn_Click"
            Grid.ColumnSpan="2"/>

    </Grid>
</Window>

这里是视觉上的区别:In designerat runtime
我该怎么办?
我试过调整大小和改变位置,但都不起作用,看起来他们像他们喜欢的那样行事。我也有点新的WPF和这并没有帮助很多。

mwngjboj

mwngjboj1#

在设计中,不仅要使用网格列,还要使用行。据我所知,你已经
1.菜单
1.一些带有按钮和文本框工具区域

  1. 4列内容区
    1.页脚状态栏
    实际上,由于在本设计中只有一个是按列划分的,因此您可能应该有一个只处理行的顶级控件,然后有一个子控件用于4列内容区域。
    根据您想要的结果的细节,顶级DockPanel、Grid或其他控件可能是最合适的。在本例中,我使用DockPanel作为主行布局。
<DockPanel LastChildFill="True">
  <Menu DockPanel.Dock="Top" Name="TopMenu" Height="20">
    <!-- menu content -->
  </Menu>

  <Grid DockPanel.Dock="Top" Height="80">
    <!-- top buttons and textbox in here -->
  </Grid>

  <StatusBar DockPanel.Dock="Bottom" Name="UsernameBar" Height="35">
    <!-- status bar content -->
  </StatusBar>

  <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="4*" />
        <ColumnDefinition Width="4*" />
        <ColumnDefinition Width="4*" />
        <ColumnDefinition Width="4*" />
    </Grid.ColumnDefinitions>

    <!-- main contents in here -->
  </Grid>

</DockPanel>

基本上,当您心中的逻辑布局层次结构与xaml中实现的层次结构匹配时,布局应该开始按预期运行。
也就是说,不要相信可视化设计器(我不知道有多少人实际上使用它来进行任何非平凡的设计),它看起来仍然与运行时的结果有些不同。相反,写你想要实现的目标,然后在运行时测试结果。

相关问题