【问题标题】:XAML window doesn't look like what I wantXAML 窗口看起来不像我想要的
【发布时间】:2019-11-19 19:02:19
【问题描述】:

我正在学习 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>

欢迎任何cmets或建议。

更新

我以下面的代码为例:

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

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

【问题讨论】:

  • 网格没有可见的边框。要获得一个,请参阅stackoverflow.com/questions/2769291/…
  • 或者将一些控件放在有可见边框的网格中。
  • 我不认为是这样,这是基于一个类似的例子,它可以工作。我将用我正在分析的示例更新问题。

标签: wpf xaml


【解决方案1】:

您的 RowDefinitions 必须是这样的:

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

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

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

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

【讨论】:

    【解决方案2】:

    因为您的行高不正确。将您的 RawDefinitions 替换为:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-21
      • 2022-09-24
      • 1970-01-01
      • 2010-12-28
      相关资源
      最近更新 更多