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

时间:2011-05-10 20:30:00

标签: wpf autoresize

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

5 个答案:

答案 0 :(得分:45)

这也让我感到悲痛,AlexK帮助我看到了光明。诀窍是不要设置高度和宽度....将它们设置为AUTO并使用MARGIN来设置大小。将HORIZONTALALIGNMENTVERTICALALIGNMENT设置为STRETCH,然后锚功能正常工作。

答案 1 :(得分:5)

控制需要拉伸,这应该是它的全部:

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

Stretch取代了将双方设置为双方。

有关面板的帮助,请参阅this overview。另请参阅documentation of the layout system

大多数控件会自动拉伸,如果你有一个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>

如果向下调整窗口大小,DataGrid的ScrollBars应该出现。

答案 2 :(得分:4)

我认为没有调整大小的控件是你的自定义控件。

  • 使用DockPanel作为容器。
  • 从控件中删除显式宽度和高度属性。

如果您在VS2008中工作,则会造成不便,因为在设计人员中查看时,您的控件会折叠到最小尺寸。

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

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

<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和d:DesignHeight指定设计时间的宽度和高度。

答案 3 :(得分:1)

这个问题已经过时了,但是,因为我发现了我在这里,我想我会抛弃我的解决方案。下面的代码用于带有两个选项卡的选项卡控件,每个选项卡包含一个用于填充选项卡的控件。我删除了明确设置的宽度和高度,并在我想要调整大小的所有控件上将水平和垂直对齐设置为auto。选项卡控件与主窗口一起延伸。选项卡中的控件会拉伸以填充选项卡。这些信息来自这里的答案。我刚刚提出了一个完整的例子。

希望这对某人有用。

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

答案 4 :(得分:0)

我也遇到过这个问题。 并尝试绑定到父控件ActualHeight和ActualWidth属性,但只有通过代码而不是XAML才能执行此操作。

即 XAML

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

后面的.cs代码

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