我正在使用a ContentControl
来UserControl
动态呈现各种派生.我不能为我的生活弄清楚如何在调整父级时调整内容Window
.我发现像许多人提到这个,但它仍然不是为我工作.这是一个简单的例子:
这是Window
XAML:
<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>
Run Code Online (Sandbox Code Playgroud)
这使用资源文件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>
Run Code Online (Sandbox Code Playgroud)
以下是main 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
{
}
Run Code Online (Sandbox Code Playgroud)
最后用户控制:
<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>
Run Code Online (Sandbox Code Playgroud)
这是运行时的样子:
我在这里错过了什么?我如何让子内容的行为使其保持锚定在父级的顶部/左侧,而在父级调整大小时,底部/右侧伸展?
pok*_*oke 14
两件事情:
首先,你要删除的VerticalAlignment
和HorizontalAlignment
你的ContentControl
.设置这些将阻止内容控件在其容器内拉伸,因此Width
并且Height
受到尊重,默认情况下均为零(因此容器本身没有大小).
设置VerticalAlignment
并HorizontalAlignment
到Stretch
,或离开它,因为它是默认的,会使容器填补了网格,这是你想要的.
<ContentControl Content="{Binding Path=ChildView}" Margin="10" />
Run Code Online (Sandbox Code Playgroud)
其次,设置Width
和将Height
在其中UserControl
将其大小设置为固定大小,因此它不会自行调整.删除这些属性,用户控件默认也会拉伸,使其填充内容控件.
如果您想为设计目的设置一定的尺寸,请设置设计尺寸而不是实际控制尺寸.为此,您拥有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>
Run Code Online (Sandbox Code Playgroud)
您设置UserControl 的Height
和Width
属性.这消除了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>
Run Code Online (Sandbox Code Playgroud)
正如poke温馨地提醒我,你也必须删除VerticalAlignment="Top"
和HorizontalAlignment="Left"
<ContentControl VerticalAlignment="Top" //<--remove
HorizontalAlignment="Left" //<--remove
VerticalContentAlignment="Stretch"
HorizontalContentAlignment="Stretch"
Content="{Binding Path=ChildView}"
Margin="10"/>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12122 次 |
最近记录: |