WPF页面内容未设置为窗口大小

时间:2017-03-22 05:29:52

标签: c# wpf xaml

我制作了一个WPF应用程序。我放了一个矩形。页面覆盖整个窗口,但矩形的大小(宽度)不等于页面。

这是照片。

这是我的应用程序的源代码:

   <Page x:Class="TestWpfApplication.Page1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:TestWpfApplication"
  mc:Ignorable="d" 
  d:DesignHeight="350" d:DesignWidth="525"
  Title="Page1" Background="#FF7ACBBC" ShowsNavigationUI="False">

<Grid>
    <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" Stroke="Black" VerticalAlignment="Top" Width="525"/>
    <Label x:Name="label" Content="Heading" HorizontalAlignment="Left" Margin="222.006,25,0,0" VerticalAlignment="Top" FontFamily="Tahoma" FontSize="22"/>
    <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="160,155,0,0" TextWrapping="Wrap" Text="And rest of the content goes here" VerticalAlignment="Top" FontFamily="Tahoma" FontSize="14"/>

</Grid>

并且

 <Window x:Class="TestWpfApplication.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:TestWpfApplication"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <local:Page1/>
</Grid>

最后App.xaml

 <Application x:Class="TestWpfApplication.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:TestWpfApplication"
         StartupUri="page1.xaml">
<Application.Resources>

</Application.Resources>

请帮助我....

3 个答案:

答案 0 :(得分:0)

将Window的SizeToContent属性设置为SizeToContent =“WidthAndHeight” SizeToContent enter image description here

答案 1 :(得分:0)

<强> Page1.Xaml.cs:

public Page1()
    {
        InitializeComponent();
        this.SizeToContent = SizeToContent.WidthAndHeight;
    }

有关详细信息,请参阅Window.SizeToContent Property

答案 2 :(得分:0)

我不知道您为什么要为矩形指定尺寸参数,并希望它适合您网页的尺寸。只需删除所有这些参数,它就会延伸。

<Rectangle Fill="#FFF4F4F5"/>