我不得不將 HTA 移動到 Powershell/WPF,現在,無論好壞,我都需要保持 UI 不變。所以...請不要對美學、頁面元素的使用等發表任何評論。我們將在 v2 中處理這些內容!
使用 Visual Studio CE 2022 v17.0.5,在設計時,我在 TabGroup 的左側和右側都獲得了我想要的 20 像素邊距。但是,在運行時,右側的邊距已經消失。
我錯過了什么?
這是 XAML:
<Window x:Class="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"
mc:Ignorable="d"
Title="Application Deployment" Icon="/DeployApplication.ico" Height="780" Width="800">
<Canvas Height="780" Width="800">
<Grid Name="grdTop">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="5"/>
<RowDefinition Height="20"/>
<RowDefinition Height="5"/>
<RowDefinition Height="20"/>
<RowDefinition Height="5"/>
<RowDefinition Height="20"/>
<RowDefinition Height="5"/>
</Grid.RowDefinitions>
<CheckBox Name="chkWriteLog" Height="18" Width="158" Grid.Column="1" Grid.Row="1" Content="_Write to a log file" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<CheckBox Name="chkSendLog" Height="18" Width="158" Grid.Column="1" Grid.Row="3" Content="_Send log via email" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<CheckBox Name="chkUseEventLog" Height="18" Width="158" Grid.Column="1" Grid.Row="5" Content="Use E_vent log" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Label Name="lblEmailAddress" Height="23" Width="200" Grid.Column="3" Grid.Row="1" Content="Select an _email address:" HorizontalContentAlignment="Right"/>
<Label Name="lblCustomEmailAddresses" Height="23" Width="200" Grid.Column="3" Grid.Row="3" Content="Custom _addresses:" HorizontalContentAlignment="Right"/>
<ListBox Name="lisEmailAddress" Height="23" Width="320" Grid.Column="5" Grid.Row="1" d:ItemsSource="{d:SampleData ItemCount=5}"/>
<TextBox Name="txtCustomEmailAddress" Height="23" Width="320" Grid.Column="5" Grid.Row="3" TextWrapping="Wrap" VerticalAlignment="Top"/>
</Grid>
<Grid Name="grdSeparator1" Canvas.Top="75">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="800"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="5"/>
<RowDefinition Height="5"/>
</Grid.RowDefinitions>
<Rectangle Grid.Column="0" Grid.Row="1" Margin="20,0,20,0" Width="780" Fill="Black" Height="3"/>
</Grid>
<Grid Name="grdTabContainer" Canvas.Top="95">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="800"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="605"/>
<RowDefinition Height="5"/>
</Grid.RowDefinitions>
<TabControl x:Name="tabMain" Height="600" Width="760" Grid.Column="0" Grid.Row="0" Margin="20,0,20,0" HorizontalAlignment="Left" VerticalAlignment="Top">
<TabItem Header="Application">
</TabItem>
<TabItem Header="Deployment type">
</TabItem>
<TabItem Header="Distribution">
</TabItem>
</TabControl>
</Grid>
</Canvas>
</Window>
設計時間

運行

uj5u.com熱心網友回復:
根據作業系統及其樣式,視窗在運行時將具有不同大小的邊框,而在設計時則沒有這樣的邊框。
您可以使用此視窗內容證明這一點:
<Window
...
Width="800"
Height="450"
mc:Ignorable="d">
<Grid>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
<Run Text="Width: " /><Run Text="{Binding ActualWidth, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}" />
<Run Text="Height: " /><Run Text="{Binding ActualHeight, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}" />
</TextBlock>
</Grid>
</Window>
- 設計時間:
Width: 800 Height: 434.04 - 運行:
Width: 784 Height: 411
如果您想解決這個問題(不觸及您的布局樣式),請將您的 XAML 更改為
<Window x:Class="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"
mc:Ignorable="d"
Title="Application Deployment"
Icon="/DeployApplication.ico"
SizeToContent="WidthAndHeight"> <!-- automatic resize of the window -->
<!-- set the size here -->
<Grid Width="800" Height="780">
<!-- your canvas goes here -->
</Grid>
</Window>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/424226.html
