wpf 嵌套 UserControl 的自动调整大小不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13706950/
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
Automatic resize of nested UserControl does not work
提问by MikeR
In my application I use a ContentControland dynamically fill it with a custom UserControl, using binding on Contentto bind to a FrameworkElement. [EDITED: added example code to show the problem]
在我的应用程序中,我使用 aContentControl并使用自定义动态填充它UserControl,使用 binding onContent绑定到FrameworkElement. [编辑:添加示例代码以显示问题]
After some input of Steven and Charleh I created a small MVVM project to reproduce the problem. My MainWindow now looks like that:
在 Steven 和 Charleh 的一些输入之后,我创建了一个小型 MVVM 项目来重现该问题。我的 MainWindow 现在看起来像这样:
<Window x:Class="ResizeExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:localVM="clr-namespace:ResizeExample.ViewModels"
xmlns:local="clr-namespace:ResizeExample"
Title="ResizeExample"
WindowStartupLocation="CenterScreen"
Height="459"
Width="795">
<Window.Resources>
<localVM:MainWindowViewModel x:Key="Windows1ViewModel" />
</Window.Resources>
<Grid DataContext="{StaticResource Windows1ViewModel}">
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Canvas Grid.Row="0">
<Menu DockPanel.Dock="Top"/>
<Label Content="Any Label on the right side" Canvas.Right="0" Canvas.Bottom="0"/>
</Canvas>
<Grid Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ContentControl Name="ControlCanvas" Content="{Binding Path=externalView}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/>
</Grid>
</Grid>
Setting externalViewworks fine, but it does not resize when the window is resized. Here how I implemented it in the ViewModel:
设置externalView工作正常,但在调整窗口大小时不会调整大小。这是我如何在 ViewModel 中实现它的:
private FrameworkElement _externalView;
public FrameworkElement externalView
{
get { return this._externalView; }
set
{
if (this._externalView != value)
{
this._externalView = value;
RaisePropertyChanged(() => externalView);
}
}
}
public MainWindowViewModel()
{
externalView = new UserControl1();
}
The UserControlcontains a TabControl(which should resize) and everything else is in the TabControl. The TabControlcontains some labels, text boxes and buttons (which should not resize) and a DataGrid(which should resize). At the moment the complete TabControlis minimized, because no size is set and it will not resize.
该UserControl包含TabControl(这应该调整)和其他一切是在TabControl。将TabControl包含一些标签,文本框和按钮(这不应该调整)和DataGrid(应调整)。目前完成TabControl最小化,因为没有设置大小,也不会调整大小。
<UserControl x:Class="ResizeExample.Views.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:DesignHeight="411" d:DesignWidth="805" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<TabControl HorizontalAlignment="Left" Name="tabControl1" VerticalAlignment="Top">
<TabItem Header="Tab1" Name="Tab1">
<Grid />
</TabItem>
<TabItem Header="Tab2" Name="Tab2">
<Grid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="6,6,0,0" Name="textBox1" VerticalAlignment="Top" Width="357" />
<Label Content="Input some data 1:" Height="28" HorizontalAlignment="Left" Margin="6,29,0,0" Name="label1" VerticalAlignment="Top" />
<Label Content="Input some data 2:" Height="28" HorizontalAlignment="Left" Margin="6,57,0,0" Name="label2" VerticalAlignment="Top" />
<Label Content="Input some data 3:" Height="28" HorizontalAlignment="Left" Margin="6,85,0,0" Name="label3" VerticalAlignment="Top" />
<DataGrid AutoGenerateColumns="False" MinHeight="200" HorizontalAlignment="Left" Margin="6,113,0,0" Name="releaseNotesGrid" VerticalAlignment="Top" MinWidth="780" />
</Grid>
</TabItem>
</TabControl>
</Grid>
</UserControl>
I thought the controls would resize automatically, but this is not working. While I'm new to WPF and MVVM, it could be that I missed something basic.
我认为控件会自动调整大小,但这不起作用。虽然我是 WPF 和 MVVM 的新手,但可能是我错过了一些基本的东西。
I found the following threadafter which I removed the sizes and added the alignments, but this didn't solve my problem.
我找到了以下线程,之后我删除了尺寸并添加了对齐方式,但这并没有解决我的问题。
回答by MikeR
I solved it now by testing a bit. The problem was the TabControland DataGridin the UserControl, which had Alignments that did not work with the stretch. This is now my UserControl1and it works fine for me:
我现在通过测试解决了它。问题是在TabControl和DataGrid中UserControl,其中有未与舒展的工作路线。这现在是我的UserControl1,它对我来说很好用:
<UserControl x:Class="ResizeExample.Views.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" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<TabControl HorizontalAlignment="Stretch" Name="tabControl1" VerticalAlignment="Stretch">
<TabItem Header="Tab1" Name="Tab1">
<Grid />
</TabItem>
<TabItem Header="Tab2" Name="Tab2" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch">
<Grid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="6,6,0,0" Name="textBox1" VerticalAlignment="Top" Width="357" />
<Label Content="Input some data 1:" Height="28" HorizontalAlignment="Left" Margin="6,29,0,0" Name="label1" VerticalAlignment="Top" />
<Label Content="Input some data 2:" Height="28" HorizontalAlignment="Left" Margin="6,57,0,0" Name="label2" VerticalAlignment="Top" />
<Label Content="Input some data 3:" Height="28" HorizontalAlignment="Left" Margin="6,85,0,0" Name="label3" VerticalAlignment="Top" />
<DataGrid AutoGenerateColumns="False" MinHeight="200" Margin="6,113,0,0" Name="releaseNotesGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinWidth="780" />
</Grid>
</TabItem>
</TabControl>
</Grid>
</UserControl>
Thanks to Steven and Charleh to point me to the right direction.
感谢 Steven 和 Charleh 为我指明了正确的方向。
回答by Charleh
In addition to Stevens answer, you may also need to set ContentControl.HorizontalContentAlignmentand ContentControl.VerticalContentAlignmentto Stretchas ContentControldefaults are Leftand Toprespectively (as far as I recall)
除了史蒂文斯回答,您可能还需要设置ContentControl.HorizontalContentAlignment并ContentControl.VerticalContentAlignment以Stretch作为ContentControl默认值Left,并Top分别(据我记得)
Otherwise even though the ContentControlwill fill the parent container, the content won't
否则即使ContentControl将填充父容器,内容也不会
Edit: Here's the default template for the ContentControlshowing the default values...
编辑:这是ContentControl显示默认值的默认模板......
<Style TargetType="ContentControl">
<Setter Property="Foreground" Value="#FF000000"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<ContentPresenter
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Cursor="{TemplateBinding Cursor}"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</ControlTemplate>
</Setter.Value>
</Setter>

