如何获取WPF ContentControl内容?

ror*_*.ap 10 .net c# wpf xaml

我正在使用a ContentControlUserControl动态呈现各种派生.我不能为我的生活弄清楚如何在调整父级时调整内容Window.我发现像许多人提到这个,但它仍然不是为我工作.这是一个简单的例子:

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

两件事情:

首先,你要删除的VerticalAlignmentHorizontalAlignment你的ContentControl.设置这些将阻止内容控件在其容器内拉伸,因此Width并且Height受到尊重,默认情况下均为零(因此容器本身没有大小).

设置VerticalAlignmentHorizontalAlignmentStretch,或离开它,因为它是默认的,会使容器填补了网格,这是你想要的.

<ContentControl Content="{Binding Path=ChildView}" Margin="10" />
Run Code Online (Sandbox Code Playgroud)

其次,设置Width和将Height在其中UserControl将其大小设置为固定大小,因此它不会自行调整.删除这些属性,用户控件默认也会拉伸,使其填充内容控件.

如果您想为设计目的设置一定的尺寸,请设置设计尺寸而不是实际控制尺寸.为此,您拥有d包含DesignWidthDesignHeight属性的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)


Dom*_*see 6

您设置UserControl 的HeightWidth属性.这消除了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)