WPF - 当我调整窗口大小时按钮不会移动

3
这是XAML代码。存在两个问题。首先,当窗口打开时,两个按钮底部都被裁剪掉了,“Save_Search”按钮的右侧也被裁剪掉了。第二个问题是当我调整窗口大小时,列表视图会像我想要的那样变长,但两个按钮最终会位于列表视图中间,而不是相对于窗口边框移动。在搜索时没有找到太多相关信息。

    Title="Queries" Height="274" Width="540">



<DockPanel Height="Auto" HorizontalAlignment="Stretch" Name="dockPanel1" VerticalAlignment="Stretch" Width="Auto">
    <Grid Height="Auto" Width="Auto" Margin="0,0,0,0">

        <TextBox Height="27" HorizontalAlignment="Left" Margin="256,6,0,0" Name="SearchTermsTextBox" VerticalAlignment="Top" Width="227" 
                  Text="Enter search terms separated by `;`"/>

        <ListView Name="ResultsListView" Margin="6,41,13,48"    ItemsSource="{Binding}" Width="auto">
            <ListView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock FontSize="15" FontWeight="Bold" Text="{Binding Name}"/>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>
            </ListView.GroupStyle>

            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="BorderThickness" Value="1" />
                    <Setter Property="Width" Value="Auto" />
                    <Setter Property="FontSize" Value="10.4"  />
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>


        <Button Content="Save Search" Margin="425,203,12.6,17.6"  Name="Save_Search"  Width="96" Height="25"  />
        <Button Name="Query_Button" Content="Ports" Margin="310,203,127.6,0" Height="25" Width="96" VerticalAlignment="Top"></Button>
    </Grid>


</DockPanel>

1个回答

6
默认情况下,HorizontalAlignmentVerticalAlignment都设置为根据给定的边距拉伸控件。这会导致在您的情况下出现不正确的行为,因为控件绑定到边距中指定的位置,尽管网格被调整大小。
为了修复它,请将HorizontalAlignment设置为Right,并将VerticalAlignment设置为Bottom。这将使按钮保持在右侧和底部边框上。
您还应修改边距值,将左和顶部边距的值设置为0,以便即使Grid的大小小于425, 203,也可以显示按钮。否则,可能只有部分按钮或没有按钮可见。
尝试使用以下代码来创建按钮:
<Button Content="Save Search" Margin="0,0,12.6,17.6"  Name="Save_Search"  Width="96" Height="25" HorizontalAlignment="Right" VerticalAlignment="Bottom" />
<Button Name="Query_Button" Content="Ports" Margin="0,0,127.6,0" Height="25" Width="96" HorizontalAlignment="Right" VerticalAlignment="Bottom"></Button>

好的,所以我不应该指定顶部和左侧边距。谢谢。 - David Green
你还应该指定水平和垂直对齐方式,使按钮粘在右侧和底部边框上 :)。 - Lukasz M
非常有帮助。谢谢! - mack

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接