如何使控件在 wpf 中调整大小,相当于锚点/停靠点属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5956073/
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-08 22:50:50  来源:igfitidea点击:

how to make controls resize in wpf, equivelant of anchor/dock properties

wpfautoresize

提问by Mike

I've read so many solutions this problem. every one of them fails to solve my problem. no matter what container I put the control into or what properties I set on the control/container it will not budge. I have a scroll viewer with a control within. I want it to resize with the window when the user resizes it at runtime. all I need is anchor=top, bottom, left, right. I don't understand why this is so elusive in WPF and why container objects and all kinds of property assignments need to be involved to accomplish what a single property can in Forms. but every solution to this problem still results in my control staying at exactly its design time size as the window is resized at runtime. what's the simple way to get a grip on dynamic control sizing in wpf?

我已经阅读了很多解决此问题的方法。他们每个人都无法解决我的问题。无论我将控件放入哪个容器或在控件/容器上设置什么属性,它都不会让步。我有一个带有控件的滚动查看器。当用户在运行时调整窗口大小时,我希望它随窗口调整大小。我只需要锚=顶部,底部,左侧,右侧。我不明白为什么这在 WPF 中如此难以捉摸,以及为什么需要涉及容器对象和各种属性分配来完成表单中单个属性的功能。但是这个问题的每个解决方案仍然会导致我的控件在运行时调整窗口大小时保持在其设计时大小。在 wpf 中掌握动态控件大小的简单方法是什么?

回答by www.redware.com

This has caused me grief as well and AlexK helped me see the light. The trick is NOT to set the Height and Width.... Set these to AUTOand use the MARGINto set the size. Set the HORIZONTALALIGNMENTand VERTICALALIGNMENTto STRETCHand then the anchor functionality works.

这也让我感到悲伤,AlexK 帮助我看到了曙光。诀窍是不要设置高度和宽度...。将这些设置为AUTO并使用MARGIN来设置大小。设置HORIZONTALALIGNMENTVERTICALALIGNMENTSTRETCH,然后锚功能的作品。

回答by H.B.

The control needs to stretch, that's all there should be to it:

控件需要拉伸,这就是它应该做的:

<MyControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

Stretchreplaces setting achors to both respective sides.

Stretch替换对双方的设置锚点。

For help on panels see this overview. Also see the documentation of the layout system.

有关面板的帮助,请参阅此概述。另请参阅布局系统文档

Most controls automatically stretch, if you have a DataGrid it should stretch too, try this sample in Kaxaml, it contains a DataGrid and a TextBlock which shows its size:

大多数控件会自动拉伸,如果你有一个 DataGrid 它也应该拉伸,在Kaxaml试试这个示例,它包含一个 DataGrid 和一个显示其大小的 TextBlock:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <DataGrid Name="grid">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
                <DataGridTextColumn Binding="{Binding Tag}" Header="Occupation"/>
            </DataGrid.Columns>
            <FrameworkElement Name="Skeet" Tag="Programmer"/>
            <FrameworkElement Name="Gravell" Tag="Programmer"/>
            <FrameworkElement Name="Steve" Tag="Coffee Getter"/>
        </DataGrid>
        <TextBlock Grid.Row="1">
            <TextBlock.Text>
                <MultiBinding StringFormat="{}{0}, {1}">
                    <Binding ElementName="grid" Path="ActualWidth"/>
                    <Binding ElementName="grid" Path="ActualHeight"/>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </Grid>
</Window>

If you size the window down the DataGrid's ScrollBars should appear.

如果您缩小窗口的大小,应该会出现 DataGrid 的 ScrollBars。

回答by AlexK

I assume the control that does not resize is your custom control.

我假设不调整大小的控件是您的自定义控件。

  • Use DockPanel as a container.
  • Remove explicit Width and Height properties from your control.
  • 使用 DockPanel 作为容器。
  • 从控件中删除显式的 Width 和 Height 属性。

If you work in VS2008, then this causes inconvenience, because you control would collapse to the minimal size when viewed in the designer.

如果你在 VS2008 中工作,那么这会造成不便,因为你的控件在设计器中查看时会折叠到最小尺寸。

Expressions Blend and starting from VS2010 both respect designer namespace, so you can specify design time only control size.

表达式 Blend 和从 VS2010 开始都尊重设计器命名空间,因此您可以指定设计时仅控制大小。

For that add the following to your control:

为此,将以下内容添加到您的控件中:

<UserControl ...
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    mc:Ignorable="d"
    d:DesignWidth="WWW" d:DesignHeight="HHH">
    ....
</UserControl>

d:DesignWidth and d:DesignHeight specify the design time width and height.

d:DesignWidth 和 d:DesignHeight 指定设计时的宽度和高度。

回答by Tony Heflin

This question is old but, since I found my here I thought I would throw out my solution. The code below is for a tab control with two tabs and each tab contains a control to fill the tab. I removed the explicitly set width and height, and set the horizontal and vertical alignments to auto on all the controls I wanted to resize. The tab control stretches wit the main window. The controls in the tabs stretch to fill the tabs. The information came from the answers here. I just put up a complete example.

这个问题很老但是,因为我在这里找到了我的答案,所以我想我会放弃我的解决方案。下面的代码适用于具有两个选项卡的选项卡控件,每个选项卡都包含一个用于填充选项卡的控件。我删除了明确设置的宽度和高度,并将所有我想调整大小的控件的水平和垂直对齐方式设置为自动。选项卡控件与主窗口一起延伸。选项卡中的控件会拉伸以填充选项卡。信息来自这里的答案。我只是举了一个完整的例子。

Hope this is useful to someone.

希望这对某人有用。

        <TabControl HorizontalAlignment="Stretch" 
                Margin="91,0,0,0" Name="tabControl1"
                VerticalAlignment="Stretch" >
        <TabItem Header="DataGrid" Name="tabItem1">
            <Grid>
                <DataGrid AutoGenerateColumns="False" 
                          HorizontalAlignment="Stretch" 
                          Name="dgFTPLog"
                          VerticalAlignment="Stretch" 
                          Margin="6,6,0,0" />
            </Grid>
        </TabItem>
        <TabItem Header="Log" Name="tabItem2">
            <Grid>
                <TextBox 
                         HorizontalAlignment="Stretch" 
                         Margin="6,6,0,0" Name="txtLog"
                         VerticalAlignment="Stretch"
                         VerticalScrollBarVisibility="Auto" 
                         HorizontalScrollBarVisibility="Auto" 
                         TextChanged="txtLog_TextChanged" />
            </Grid>
        </TabItem>
    </TabControl>

回答by Arash

I've came across this issues as well. and Attempted at binding to parent controls ActualHeight and ActualWidth properties except this only works if you do it via code behind not by XAML.

我也遇到过这个问题。并尝试绑定到父控件的 ActualHeight 和 ActualWidth 属性,除非这仅在您通过隐藏代码而非 XAML 执行此操作时才有效。

i.e XAML

即 XAML

<MyParentControl x:name="parentControl" SizeChanged="parentControl_SizeChanged">
     <ChildControl x:name=childControl" />
</MyParentControl>

in the .cscode behind

.cs后面的代码中

private void parentControl_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        childControl.Height = parentControl.ActualHeight;
        childControl.Width = parentControl.ActualWidth;
    }