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

huangapple go评论59阅读模式
英文:

WPF controls in preview don't match dimension and position at runtime - How to fix?

问题

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&gt>

        <!-- 这里是您的XAML控件定义 -->

    &lt;/Grid&gt;
&lt;/Window&gt;

视觉上的不同是:
在设计器中运行时

您可以尝试调整控件的布局属性,确保它们按照您的预期进行布局和大小调整。此外,检查您的代码是否包含任何动态更改控件位置或大小的逻辑,这可能导致不一致的外观。鉴于您对WPF相对新,可能需要花些时间来深入了解布局和控件属性的工作方式。

英文:

Controls dimension and position in my WPF preview window don't match the actual window in runtime. I don't know why it does this and if it is related to code or my screen. Here is the code:

&lt;Window x:Name=&quot;HomeWindow&quot; x:Class=&quot;Progetto_Amada.MainWindow&quot; x:FieldModifier=&quot;public&quot;
xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
xmlns:MyNamespace=&quot;clr-namespace:Progetto_Amada&quot;
xmlns:local=&quot;clr-namespace:Progetto_Amada&quot;
mc:Ignorable=&quot;d&quot;
Title=&quot;Amada Reminder for General Administration&quot; MinWidth=&quot;1100&quot; MinHeight=&quot;650&quot; Background=&quot;#FF323232&quot; Loaded=&quot;HomeWindow_Loaded&quot;  WindowState=&quot;Maximized&quot; HorizontalAlignment=&quot;Left&quot; SizeToContent=&quot;WidthAndHeight&quot;
&gt;
&lt;Grid&gt;
&lt;Grid.ColumnDefinitions&gt;
&lt;ColumnDefinition Width=&quot;4*&quot; /&gt;
&lt;ColumnDefinition Width=&quot;4*&quot; /&gt;
&lt;ColumnDefinition Width=&quot;4*&quot; /&gt;
&lt;ColumnDefinition Width=&quot;4*&quot; /&gt;
&lt;/Grid.ColumnDefinitions&gt;
&lt;Menu Name=&quot;TopMenu&quot;
Height=&quot;20&quot;
VerticalAlignment=&quot;Top&quot;
Background=&quot;#FF323232&quot;
Foreground=&quot;White&quot;
Grid.ColumnSpan=&quot;4&quot;&gt;
&lt;MenuItem
Header=&quot;File&quot;
FontFamily=&quot;Arial&quot;&gt;
&lt;MenuItem
Name=&quot;AddFile&quot;
Header=&quot;Allega un file&quot;
Foreground=&quot;Black&quot;
Click=&quot;CaricaFile_Click&quot;&gt;
&lt;MenuItem.Icon&gt;
&lt;Image Source=&quot;C:\Users\david\Desktop\progetto-pcto-requests\Progetto-Amada\bin\Debug\net6.0-windows\assets\file.png&quot; /&gt;
&lt;/MenuItem.Icon&gt;
&lt;/MenuItem&gt;
&lt;MenuItem Name=&quot;RemoveFile&quot;
Header=&quot;Rimuovi un file&quot;
Foreground=&quot;Black&quot;
Click=&quot;RimuoviFile_Click&quot;&gt;
&lt;MenuItem.Icon&gt;
&lt;Image Source=&quot;C:\Users\david\Desktop\progetto-pcto-requests\Progetto-Amada\bin\Debug\net6.0-windows\assets\file.png&quot; /&gt;
&lt;/MenuItem.Icon&gt;
&lt;/MenuItem&gt;
&lt;/MenuItem&gt;
&lt;MenuItem
Header=&quot;Visualizza&quot;
FontFamily=&quot;Arial&quot;&gt;
&lt;MenuItem
Name=&quot;VediContratti&quot;
Header=&quot;Visualizza tutti i contratti&quot;
Foreground=&quot;Black&quot;
Click=&quot;TuttiIContratti_Click&quot;&gt;
&lt;MenuItem.Icon&gt;
&lt;Image Source=&quot;C:\Users\david\Desktop\progetto-pcto-requests\Progetto-Amada\bin\Debug\net6.0-windows\assets\agreement.png&quot; /&gt;
&lt;/MenuItem.Icon&gt;
&lt;/MenuItem&gt;
&lt;MenuItem
Name=&quot;VediMacchine&quot;
Header=&quot;Visualizza tutte le macchine&quot;
Foreground=&quot;Black&quot;
Click=&quot;TutteLeMacchine_Click&quot;&gt;
&lt;MenuItem.Icon&gt;
&lt;Image Source=&quot;C:\Users\david\Desktop\progetto-pcto-requests\Progetto-Amada\bin\Debug\net6.0-windows\assets\machine.png&quot; /&gt;
&lt;/MenuItem.Icon&gt;
&lt;/MenuItem&gt;
&lt;/MenuItem&gt;
&lt;MenuItem
Header=&quot;Utente&quot;
FontFamily=&quot;Arial&quot;&gt;
&lt;MenuItem
Name=&quot;NomeUtente&quot;
Header=&quot;Utente: [ Nome utente ]&quot;
Foreground=&quot;Black&quot;
IsEnabled=&quot;False&quot;&gt;
&lt;/MenuItem&gt;
&lt;MenuItem
Name=&quot;InfoUtente&quot;
Header=&quot;Informazioni sull&#39;utente&quot;
Foreground=&quot;Black&quot;&gt;
&lt;MenuItem.Icon&gt;
&lt;Image Source=&quot;C:\Users\david\Desktop\progetto-pcto-requests\Progetto-Amada\bin\Debug\net6.0-windows\assets\user.png&quot; /&gt;
&lt;/MenuItem.Icon&gt;
&lt;/MenuItem&gt;
&lt;MenuItem Name=&quot;AccessoCambio&quot; Header=&quot;Cambia utente&quot; Foreground=&quot;Black&quot; Click=&quot;CambiaUtente_Click&quot;&gt;
&lt;MenuItem.Icon&gt;
&lt;Image Source=&quot;C:\Users\david\Desktop\progetto-pcto-requests\Progetto-Amada\bin\Debug\net6.0-windows\assets\user.png&quot; /&gt;
&lt;/MenuItem.Icon&gt;
&lt;/MenuItem&gt;
&lt;/MenuItem&gt;
&lt;/Menu&gt;
&lt;Button
Grid.Column=&quot;3&quot;
x:Name=&quot;btnCerca&quot;
Content=&quot;CERCA&quot;
HorizontalAlignment=&quot;Right&quot;
Margin=&quot;0,40,20,0&quot;
VerticalAlignment=&quot;Top&quot;
Height=&quot;36&quot;
Width=&quot;80&quot;
Click=&quot;btnCerca_Click&quot;
Grid.ColumnSpan=&quot;4&quot;
FontWeight=&quot;Bold&quot;
FontFamily=&quot;Arial&quot;/&gt;
&lt;Label
Grid.Column=&quot;0&quot;
Content=&quot;Scadenze Imminenti:&quot;
HorizontalAlignment=&quot;Left&quot;
Margin=&quot;8,100,0,0&quot;
VerticalAlignment=&quot;Top&quot;
Foreground=&quot;White&quot;
FontSize=&quot;25&quot;
/&gt;
&lt;Label Grid.Column=&quot;1&quot;
Content=&quot;Scadenze Urgenti:&quot;
HorizontalAlignment=&quot;Left&quot;
Margin=&quot;8,100,0,0&quot;
VerticalAlignment=&quot;Top&quot;
Foreground=&quot;White&quot;
FontSize=&quot;25&quot;
/&gt;
&lt;Label Grid.Column=&quot;2&quot;
Content=&quot;Scadenze Remote:&quot;
HorizontalAlignment=&quot;Left&quot;
Margin=&quot;8,100,0,0&quot;
VerticalAlignment=&quot;Top&quot;
Foreground=&quot;White&quot;
FontSize=&quot;25&quot;
/&gt;
&lt;Label Grid.Column=&quot;3&quot;
Content=&quot;Tutte le scadenze:&quot;
HorizontalAlignment=&quot;Left&quot;
Margin=&quot;6,100,0,0&quot;
VerticalAlignment=&quot;Top&quot;
Foreground=&quot;White&quot;
FontSize=&quot;25&quot;
/&gt;
&lt;StatusBar
Name=&quot;UsernameBar&quot; 
Height=&quot;35&quot; 
VerticalAlignment=&quot;Bottom&quot;
Background=&quot;Black&quot;
Foreground=&quot;White&quot;
Grid.ColumnSpan=&quot;4&quot; Visibility=&quot;Visible&quot; &gt;
&lt;Label 
Name=&quot;UtenteLabel&quot; 
Content=&quot;Utente: [ Nome Utente ]&quot; 
Foreground=&quot;White&quot; 
FontSize=&quot;15&quot; 
Width=&quot;296&quot; 
Height=&quot;30&quot;
HorizontalAlignment=&quot;Center&quot;
VerticalAlignment=&quot;Center&quot; 
Visibility=&quot;Visible&quot; 
/&gt;
&lt;/StatusBar&gt;
&lt;TextBox x:Name=&quot;ricercaTextbox&quot; 
TextWrapping=&quot;Wrap&quot; 
Height=&quot;36&quot; 
VerticalAlignment=&quot;Top&quot; 
Margin=&quot;0,40,105,0&quot; 
FontSize=&quot;20&quot; 
VerticalContentAlignment=&quot;Center&quot;
Grid.Column=&quot;2&quot;
HorizontalAlignment=&quot;Right&quot;
Width=&quot;273&quot; Grid.ColumnSpan=&quot;2&quot;
/&gt;
&lt;Grid Grid.Column=&quot;0&quot; Margin=&quot;10,148,10,40&quot;&gt;
&lt;ScrollViewer&gt;
&lt;StackPanel
Name=&quot;ScadenzeImminenti&quot;
x:FieldModifier=&quot;public&quot;&gt;
&lt;/StackPanel&gt;
&lt;/ScrollViewer&gt;
&lt;/Grid&gt;
&lt;Grid Grid.Column=&quot;1&quot; Margin=&quot;10,148,10,40&quot;&gt;
&lt;ScrollViewer&gt;
&lt;StackPanel
Name=&quot;ScadenzeUrgenti&quot;
x:FieldModifier=&quot;public&quot;&gt;
&lt;/StackPanel&gt;
&lt;/ScrollViewer&gt;
&lt;/Grid&gt;
&lt;Grid Grid.Column=&quot;2&quot; Margin=&quot;10,148,10,40&quot;&gt;
&lt;ScrollViewer&gt;
&lt;StackPanel
Name=&quot;ScadenzeRemote&quot;
x:FieldModifier=&quot;public&quot;&gt;
&lt;/StackPanel&gt;
&lt;/ScrollViewer&gt;
&lt;/Grid&gt;
&lt;Grid Grid.Column=&quot;3&quot; Margin=&quot;10,148,10,40&quot;&gt;
&lt;ScrollViewer&gt;
&lt;StackPanel
Name=&quot;TutteLeScadenze&quot;
x:FieldModifier=&quot;public&quot;&gt;
&lt;/StackPanel&gt;
&lt;/ScrollViewer&gt;
&lt;/Grid&gt;
&lt;Button
Grid.Column=&quot;0&quot;
x:Name=&quot;addScadenzaBtn&quot;
Content=&quot;AGGIUNGI UNA SCADENZA&quot;
HorizontalAlignment=&quot;Left&quot;
Margin=&quot;10,40,0,0&quot;
VerticalAlignment=&quot;Top&quot;
Height=&quot;36&quot; Width=&quot;275&quot;
Click=&quot;addScadenzaBtn_Click&quot;
FontWeight=&quot;Bold&quot;
FontFamily=&quot;Arial&quot;
Grid.ColumnSpan=&quot;2&quot;/&gt;
&lt;Button
x:Name=&quot;allMacchineBtn&quot;
Content=&quot;TUTTE LE MACCHINE&quot;
HorizontalAlignment=&quot;Left&quot;
Margin=&quot;335,40,0,0&quot;
VerticalAlignment=&quot;Top&quot;
Height=&quot;36&quot;
Width=&quot;310&quot;
FontWeight=&quot;Bold&quot;
FontFamily=&quot;Arial&quot;
Click=&quot;allMacchineBtn_Click&quot;
Grid.ColumnSpan=&quot;2&quot;/&gt;
&lt;/Grid&gt;
&lt;/Window&gt; 

And here is the difference visually:
In designer and
at runtime

What can I do?

I tried resizing and changing positions, but nothing works, it seems like they act as they like to. I am also kinda new to WPF and this doesn't help a lot.

答案1

得分: 1

你的设计不仅应该使用网格列,还应该使用行。据我所见,你有:

  1. 菜单
  2. 带有按钮和文本框的某种工具区域
  3. 一个有4列的内容区域
  4. 页脚状态栏

实际上,由于在这个设计中只有一个是按列分隔的,你可能应该有一个顶级控制来处理行,然后为4列的内容区域创建一个子控制。

根据你所期望的结果的详细信息,顶级的 DockPanel、Grid 或其他一些控件可能更适合。在这个示例中,我使用 DockPanel 来进行主行布局。

<DockPanel LastChildFill="True">
  <Menu DockPanel.Dock="Top" Name="TopMenu" Height="20">
    <!-- 菜单内容 -->
  </Menu>

  <Grid DockPanel.Dock="Top" Height="80">
    <!-- 这里放置顶部按钮和文本框 -->
  </Grid>

  <StatusBar DockPanel.Dock="Bottom" Name="UsernameBar" Height="35">
    <!-- 状态栏内容 -->
  </StatusBar>

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

    <!-- 主要内容放在这里 -->
  </Grid>
</DockPanel>

基本上,当你心中的逻辑布局层次结构与在 XAML 中实现的层次结构相匹配时,布局应该会按预期运行。

话虽如此,不要完全依赖可视化设计器(我不知道有多少人实际上在非常复杂的设计中使用它),它的显示结果可能与运行时结果略有不同。相反,编写你想要实现的内容,然后在运行时测试结果。

英文:

You should not only use grid columns, but rows too in your design. As far as I can see, you have

  1. the Menu
  2. some sort of tool area with buttons and a textbox
  3. a 4-column content area
  4. a footer status bar

Actually, since only one of them is column-divided in this design, you should probably have a top-level control that only handles the rows and then have a sub-control for the 4-column content area.

Depending on the details of what your desired result should look like, a top level DockPanel, Grid or some other control might be best suited. In this example I use the DockPanel for the main row layout.

&lt;DockPanel LastChildFill=&quot;True&quot;&gt;
&lt;Menu DockPanel.Dock=&quot;Top&quot; Name=&quot;TopMenu&quot; Height=&quot;20&quot;&gt;
&lt;!-- menu content --&gt;
&lt;/Menu&gt;
&lt;Grid DockPanel.Dock=&quot;Top&quot; Height=&quot;80&quot;&gt;
&lt;!-- top buttons and textbox in here --&gt;
&lt;/Grid&gt;
&lt;StatusBar DockPanel.Dock=&quot;Bottom&quot; Name=&quot;UsernameBar&quot; Height=&quot;35&quot;&gt;
&lt;!-- status bar content --&gt;
&lt;/StatusBar&gt;
&lt;Grid&gt;
&lt;Grid.ColumnDefinitions&gt;
&lt;ColumnDefinition Width=&quot;4*&quot; /&gt;
&lt;ColumnDefinition Width=&quot;4*&quot; /&gt;
&lt;ColumnDefinition Width=&quot;4*&quot; /&gt;
&lt;ColumnDefinition Width=&quot;4*&quot; /&gt;
&lt;/Grid.ColumnDefinitions&gt;
&lt;!-- main contents in here --&gt;
&lt;/Grid&gt;
&lt;/DockPanel&gt;

Basically, when the logical layout-hierarchy that you have in mind matches the hierarchy that is implemented in xaml, the layout should start behaving as expected.

That being said, do not trust the visual designer (I don't know a lot of people who actually use it for any non-trivial design), it might still look somewhat different from the runtime result. Instead, write what you want to achieve and then test the result at runtime.

huangapple
  • 本文由 发表于 2023年5月22日 17:06:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定