简体   繁体   English

WPF-Horizo​​ntalAlignment行为

[英]WPF - HorizontalAlignment behavior

I'm trying to experiment with WPF, so I created some test window to see how it goes and I encountered a little behavior I did not expect with the HorizontalAlignment property. 我正在尝试使用WPF,因此我创建了一些测试窗口以查看其运行情况,并且遇到了Horizo​​ntalAlignment属性所没有的某些行为。

First of all the screen looks like this: 首先,屏幕如下所示:
在此处输入图片说明

The problem is, that in the bottom row of the marked grid, I wanted to have the left textBlock and CheckBox aligned to the left, the center textBlock and DatePicker aligned to the center and the same with the right. 问题是,在标记网格的底部行中,我想使左侧的textBlock和CheckBox左对齐,中央的textBlock和DatePicker对齐中心,并与右侧对齐。 As you can see in the picture earlier, it doesn't really happen. 正如您在前面的图片中所看到的,这实际上并没有发生。

The XAML for this grid row: 此网格行的XAML:

<StackPanel Grid.Row="2" Grid.ColumnSpan="2" Orientation="Horizontal">
        <TextBlock HorizontalAlignment="Left" Margin="10">Filter By Date</TextBlock>
        <CheckBox HorizontalAlignment="Left" Margin="2" Height="18" Width="13"></CheckBox>
        <TextBlock HorizontalAlignment="Center" Margin="10">Begin Date</TextBlock>
        <DatePicker HorizontalAlignment="Center" Margin="2"> </DatePicker>
        <TextBlock HorizontalAlignment="Right" Margin="10">End Date</TextBlock>
        <DatePicker HorizontalAlignment="Right" Margin="2"></DatePicker>
    </StackPanel>

How do i make them be closer to the edges and center and not just one after another like I guess is the automatic StackPanel's layout? 我如何使它们更靠近边缘和中心,而不仅仅是像我想的那样自动堆叠面板的布局?

Horizontal StackPanel will ignore HorizontalAlignment of its children but you can use Grid instead with 3 StackPanel aligned left, centre and right: Horizo​​ntal StackPanel会忽略其子级的HorizontalAlignment ,但是您可以使用Grid ,将3个StackPanel左右,左右对齐:

<Grid Grid.Row="2" Grid.ColumnSpan="2">
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
        <TextBlock Margin="10">Filter By Date</TextBlock>
        <CheckBox Margin="2" Height="18" Width="13"></CheckBox>
    </StackPanel>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
        <TextBlock Margin="10">Begin Date</TextBlock>
        <DatePicker Margin="2"> </DatePicker>
    </StackPanel>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
        <TextBlock Margin="10">End Date</TextBlock>
        <DatePicker Margin="2"></DatePicker>
    </StackPanel>
</Grid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM