在我的应用程序中,我使用ContentControl
并使用自定义UserControl
动态填充它,使用Content
上的绑定绑定到FrameworkElement
。 [已编辑:添加示例代码以显示问题]
在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>
设置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();
}
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>
我认为控件会自动调整大小,但这不起作用。虽然我是WPF和MVVM的新手,但可能是我错过了一些基本的东西。
我发现the following thread之后删除了尺寸并添加了对齐,但这并没有解决我的问题。
答案 0 :(得分:4)
我现在通过测试来解决它。问题是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>
感谢Steven和Charleh指出了正确的方向。
答案 1 :(得分:2)
除了Stevens的回答,您可能还需要将ContentControl.HorizontalContentAlignment
和ContentControl.VerticalContentAlignment
设置为Stretch
,因为ContentControl
默认值为Left
和Top
分别(据我所记)
否则,即使ContentControl
将填充父容器,内容也不会
编辑:这是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>
答案 2 :(得分:1)