首页 > 解决方案 > XAML 窗口看起来不像我想要的

问题描述

我正在学习 WPF,我正在 XAML 中创建一个窗口。

窗口应如下所示:

在此处输入图像描述

但是当我运行程序时,它看起来像这样:

在此处输入图像描述

代码如下:

<Page x:Class="WpfApp1.ProductsManagement"
      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:WpfApp1"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="ProductsManagement">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="80" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="300" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="420" />
        </Grid.RowDefinitions>
            <TextBlock 
            Margin="5"
                Text="Search" 
                  Grid.Row="0"
            Grid.Column="0"/>
            <TextBox
                Margin="5"
                Grid.ColumnSpan="2"
                Grid.Column="1"
                Background ="White"
                  Grid.Row="0"
                Text="hi"/>
            <DataGrid
                Margin ="5"
                Name="dataGrid"
            Grid.Column="0"
                Grid.ColumnSpan="2"
                Grid.Row="1"/>
            <Border
                 Margin ="5"
                Grid.Row="1"
                Grid.Column="2"/>

    </Grid>
</Page>

欢迎任何意见或建议。

更新

我以下面的代码为例:

<Page x:Class="WpfApp1.Discussion"
      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:data="clr-namespace:BikeShop"
      xmlns:local="clr-namespace:WpfApp1"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Discussion">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="100" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="50" />
        </Grid.RowDefinitions>
        <ListBox 
            Grid.ColumnSpan="2" 
            Margin="5"/>
        <Button 
            Grid.Row="1"
            Grid.Column="1"
            Margin="5"
            Content="Send" />
        <TextBox 
            Grid.Row="1"
            Margin="5"
            Text="Type your message here" />
    </Grid>
</Page>

当我运行代码时,它看起来像这样:(它工作正常)

在此处输入图像描述

标签: wpfxaml

解决方案


您的 RowDefinitions 必须是这样的:

<Grid.RowDefinitions>
     <RowDefinition Height="40"/>
     <RowDefinition Height="*"/>
</Grid.RowDefinitions>

现在你要求为第一行填充整个页面并将第二行设置为 420 的高度。

您必须为第一个定义特定值,为第二个定义 *。

您在设计器中看不到错误,因为您将第二行设置为 420。显然您在 30 处看到了第一行。但是当您进入全屏时,第一行变得更大。


推荐阅读