1,由于项目要求需要使用绝对定位
window:宽度:500 高度:300 + Canvas为容器控件,用于定位
<Window x:Class="WindowsHeight.Views.PrismWindow1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
Loaded="Window_Loaded"
Title="{Binding Title}"
prism:ViewModelLocator.AutoWireViewModel="True"
Width="500"
Height="350">
<Canvas Background="Bisque" x:Name="canvas">
<Label Canvas.Top="10" x:Name="lx1" Canvas.Left="60" Height="30" Width="100" BorderBrush="Red" Content="定义宽度:500"/>
<Label Canvas.Top="40" x:Name="ly1" Canvas.Left="60" Height="30" Width="100" BorderBrush="Red" Content="定义高度:350"/>
<Label Canvas.Top="90" x:Name="lx" Canvas.Left="60" Height="30" Width="100" BorderBrush="Red"/>
<Label Canvas.Top="130" x:Name="ly" Canvas.Left="60" Height="30" Width="100" BorderBrush="Red"/>
<Button Canvas.Top="320" Height="20" Canvas.Left="100" Content="测试"></Button>
</Canvas>
</Window>
后台代码:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
lx.Content = "窗体宽度:" + this.Width;
ly.Content = "窗体高度:" + this.Height;
}
运行效果如下,测试控件没有显示出来:
原因:一番搜索,原来窗体的高度包括标题栏,边框等等
2,代码如下调整,
1,不设置窗体的高度高度 添加属性 SizeToContent="WidthAndHeight"
2,设置Canvas容器控件的高度和宽度
<Window x:Class="WindowsHeight.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
Loaded="Window_Loaded"
Title="{Binding Title}" SizeToContent="WidthAndHeight">
<Canvas Background="Bisque" Width="500" Height="350" x:Name="canvas">
<Label Canvas.Top="10" x:Name="lx1" Canvas.Left="60" Height="30" Width="100" BorderBrush="Red" Content="定义宽度:500"/>
<Label Canvas.Top="40" x:Name="ly1" Canvas.Left="60" Height="30" Width="100" BorderBrush="Red" Content="定义高度:350"/>
<Label Canvas.Top="90" x:Name="lx" Canvas.Left="60" Height="30" Width="100" BorderBrush="Red"/>
<Label Canvas.Top="130" x:Name="ly" Canvas.Left="60" Height="30" Width="100" BorderBrush="Red"/>
<Button Canvas.Top="320" Height="20" Canvas.Left="100" Content="测试"></Button>
</Canvas>
</Window>
运行效果如下,测试按钮显示出来了 窗体的高度和宽度自动动态调整了