如何让 WPF ContentControl 内容拉伸?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31146828/
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
How to get WPF ContentControl content to stretch?
提问by rory.ap
I'm using a ContentControlto render various UserControlderivations dynamically. I can't for the life of me figure out how to get the content to stretch when I resize the parent Window. I've found many references like this, but it's still not working for me. Here is a simple example:
我正在使用 a动态ContentControl呈现各种UserControl派生。我一生都无法弄清楚如何在调整 parent 大小时使内容拉伸Window。我发现像许多人提到这个,但它仍然不是对我来说有效。这是一个简单的例子:
This is the WindowXAML:
这是WindowXAML:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary Source="Dictionary1.xaml"/>
</Window.Resources>
<Grid>
<ContentControl VerticalAlignment="Top"
HorizontalAlignment="Left"
VerticalContentAlignment="Stretch"
HorizontalContentAlignment="Stretch"
Content="{Binding Path=ChildView}"
Margin="10"/>
</Grid>
</Window>
This uses the resource file Dictionary1.XAML:
这使用资源文件Dictionary1.XAML:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModels ="clr-namespace:WpfApplication3"
xmlns:views ="clr-namespace:WpfApplication3">
<DataTemplate DataType="{x:Type viewModels:UserControlViewModel}">
<views:UserControl1/>
</DataTemplate>
</ResourceDictionary>
Here is the code behind for the main Windowas well as the view model classes:
这是主要Window和视图模型类的代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainViewModel();
}
}
public class MainViewModel
{
public UserControlViewModel ChildView { get; set; }
public MainViewModel()
{
ChildView = new UserControlViewModel();
}
}
public class UserControlViewModel
{
}
and finally the user control:
最后是用户控制:
<UserControl x:Class="WpfApplication3.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Background="Blue"
Height="141" Width="278"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch">
<Grid>
</Grid>
</UserControl>
Here's what it looks like at run time:
这是运行时的样子:




What am I missing here? How can I get the child content to behave such that it remains anchored to the top/left of the parent and the bottom/right stretches as the parent is resized?
我在这里错过了什么?我怎样才能让子内容的行为使其保持锚定在父级的顶部/左侧,而底部/右侧在父级调整大小时伸展?
回答by poke
Two things:
两件事情:
First, you want to remove the VerticalAlignmentand HorizontalAlignmenton your ContentControl. Setting these will prevent the content control from stretching within its container, so Widthand Heightare respected, which are both zero by default (so the container has no size on its own).
首先,你要删除的VerticalAlignment和HorizontalAlignment你的ContentControl。设置这些将防止内容控件在其容器内拉伸,因此Width和Height受到尊重,默认情况下它们都为零(因此容器本身没有大小)。
Setting VerticalAlignmentand HorizontalAlignmentto Stretch, or leaving it out since it's the default, will make the container fill the grid, which is what you want.
设置VerticalAlignment和HorizontalAlignmentto Stretch,或者因为它是默认值而将其省略,将使容器填充网格,这就是您想要的。
<ContentControl Content="{Binding Path=ChildView}" Margin="10" />
Second, setting Widthand Heightwithin the UserControlwill set its size to that fixed size, so it will not adjust itself. Remove those attributes and the user control will default to stretch too, making it fill the content control.
其次,设置Width和Height内UserControl会将其大小设置为固定大小,因此不会自行调整。删除这些属性,用户控件也将默认拉伸,使其填充内容控件。
If you want to have a certain size for design purposes, then set the design sizeinstead of the actual control size. For that, you have the dXAML namespace which contains DesignWidthand DesignHeightproperties. Setting these will affect the designer but they are ignored later when the view is rendered.
如果为了设计目的想要有一定的尺寸,那么设置设计尺寸而不是实际的控件尺寸。为此,您拥有d包含DesignWidth和DesignHeight属性的XAML 命名空间。设置这些将影响设计器,但稍后在渲染视图时会忽略它们。
<UserControl x:Class="WpfApplication3.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d"
d:DesignWidth="400" d:DesignHeight="250"
Background="Blue">
…
</UserControl>
回答by Domysee
You set the Heightand Widthproperty of the UserControl. This removes any leeway the WPF layouting has. So it does the best thing it can, which is centering the UserControl. If you remove the width and height, it should stretch as you expect.
您设置UserControl的HeightandWidth属性。这消除了 WPF 布局的任何余地。所以它尽其所能,即以 UserControl 为中心。如果您删除宽度和高度,它应该按您的预期拉伸。
<UserControl x:Class="WpfApplication3.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Background="Blue"
Height="141" Width="278" //<-- remove
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch">
<Grid>
</Grid>
</UserControl>
As poke kindly reminded me, you also have to remove VerticalAlignment="Top"and HorizontalAlignment="Left"
正如 poke 好心地提醒我的那样,您还必须删除VerticalAlignment="Top"和HorizontalAlignment="Left"
<ContentControl VerticalAlignment="Top" //<--remove
HorizontalAlignment="Left" //<--remove
VerticalContentAlignment="Stretch"
HorizontalContentAlignment="Stretch"
Content="{Binding Path=ChildView}"
Margin="10"/>

