WPF - 当我调整窗口按钮不移动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12716413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:38:10  来源:igfitidea点击:

WPF - When I Resize A Window Buttons Don't Move

wpfxaml

提问by David Green

Here's the XAML. Two problems. Frst when the window opens, the 2 buttons are both chopped off at the bottom and 'Save_Search' button is chopped off at the right side. Second problem is when I resize the window the list view gets longer as I intended but the buttons both end up in the middle of the list view, instead of moving in relation to the window border. There wasn't much when I searched on this.

这是 XAML。两个问题。首先,当窗口打开时,底部的 2 个按钮都被砍掉了,而右侧的“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>

回答by Lukasz M

By default HorizontalAlignmentand VerticalAlignmentare both set to stretch the control based on the given margins. This causes incorrect behaviour in your case, because controls are bound to the position specified in margins despite the grid being resized.

默认情况下HorizontalAlignmentVerticalAlignment两者都设置为根据给定的边距拉伸控件。这会在您的情况下导致不正确的行为,因为尽管调整了网格的大小,但控件仍绑定到边距中指定的位置。

In order to fix it, set HorizontalAlignmentto Rightand VerticalAlignmentto Bottom. This will make the button to stick to the right and bottom borders.

为了解决这个问题,设置HorizontalAlignmentRightVerticalAlignmentBottom。这将使按钮粘在右侧和底部边框上。

You should also modify margin value to have value 0for both left and top margins, so the buttons can be displayed even if Gridsize is less than 425, 203. Otherwise, only part of the button or no button may be visible.

您还应该修改边距值0,使左边距和上边距都有值,这样即使Grid大小小于425, 203也可以显示按钮。否则,可能只有按钮的一部分或没有按钮可见。

Try to use the following code for the buttons:

尝试对按钮使用以下代码:

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