Qt程序员的xaml布局介绍

时间:2013-02-17 17:25:31

标签: wpf qt xaml layout

我习惯了Qt及其Qt Designer。

XAML及其布局控件'grid'和'StackPanel'在某种程度上相似,但我仍然错过或没有找到Qt中设计的一些最常见的属性。由于我是XAML的新手,我想知道答案或进一步提供的文件。

例如,我想添加2个元素(比如按钮)水平或垂直的默认高度和最小高度和最小宽度。如果水平对齐,则应将其推向左侧,而右侧的剩余侧应为自由。这意味着如果窗口大小增加,按钮的大小不会增加。在Qt中,这是通过网格与间隔符结合实现的(例如,参见this tutorial)。

我的第一个XAML远离我的期望。 RowDefinition的定义是从教程中复制的。但是我不明白它的意思......

<Window x:Class="SpectrumViewer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Spectrum Viewer" Height="350" Width="525">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="*" />
      <RowDefinition Height="Auto" />      
    </Grid.RowDefinitions>
    <StackPanel Orientation="Horizontal" Margin="8" VerticalAlignment="Top">
      <Button MinWidth="75" MaxHeight="23" Margin="10" HorizontalAlignment="Left" Width="100" Name="buttonTest">test</Button>
      <TextBox MinWidth="75" MaxHeight="23" Margin="10" HorizontalAlignment="Left" Width="150" Name="textBoxValue"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal" Margin="8" >
      <Button MinWidth="75" MaxHeight="23" Margin="10" HorizontalAlignment="Left" Width="100" Name="button1">button 1</Button>
      <Button MinWidth="75" MaxHeight="23" Margin="10" HorizontalAlignment="Left" Width="100" Name="button2">button 2</Button>
    </StackPanel>
  </Grid>
</Window>

下一张图片显示元素被推向左侧,但两个堆叠的面板彼此上方未被推到顶部。如果我用VerticalAlignment="Top"设置它们,它们会重叠,这也是错误的。 enter image description here

如果我调整窗口大小,可以看到元素没有调整大小,第二个StackPanel与第一个重叠:

enter image description here

相反,元素应调整到最小宽度,并且应禁止进一步调整窗口大小。

1 个答案:

答案 0 :(得分:3)

VerticalAlignment设置为Top时,两个堆栈面板重叠的第一个问题是因为您没有指定它们应该是Grid.Row,所以默认为都在Grid.Row = 0。指定行索引,它们不会重叠:

<StackPanel Grid.Row="0" Orientation="Horizontal" Margin="8" VerticalAlignment="Top">
  ....
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="8" VerticalAlignment="Top">
  ....
</StackPanel>

当达到按钮的最小尺寸时,窗口无法停止调整大小。您可以设置MinWidth的{​​{1}}并让窗口停在那里:

Grid

或者设置<Window x:Class="SpectrumViewer.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Spectrum Viewer" Height="350" Width="525" MinWidth="{Binding RelativeSource={RelativeSource Self}, Path=Content.MinWidth}"> <Grid MinWidth="350"> ....... </Grid> </Window> 本身的MinWidth

相关问题