WPF-在UserControl中托管内容

WPF-在UserControl中托管内容,wpf,user-controls,Wpf,User Controls,我正在尝试创建一个用户控件,它有一个带有两行的网格。 第一行表示标题,第二行表示将在用户控件之外定义的内容,如本例中的按钮 不知怎的,我没有让它工作 UserControl1 xaml: <Grid Background="LightBlue"> <Grid.RowDefinitions> <RowDefinition Height="50" /> <RowDefinition Height="*" />

我正在尝试创建一个用户控件,它有一个带有两行的
网格。
第一行表示标题,第二行表示将在用户控件之外定义的内容,如本例中的
按钮

不知怎的,我没有让它工作

UserControl1 xaml:

  <Grid Background="LightBlue">
    <Grid.RowDefinitions>
        <RowDefinition Height="50" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <TextBlock Text="Title" FontSize="30" Margin="10,0,0,0"/>
</Grid>

主窗口xaml:

 <Grid>
    <local:UserControl1>
        <Button>Click me</Button>
    </local:UserControl1>
</Grid>

点击我
下面的图片应该可以解释我的问题: 以下代码

<local:UserControl1>
    <Button>Click me</Button>
</local:UserControl1>
并向其标记中添加一些元素以承载附加内容。下面是一个扩展您提供的标记的示例:

<UserControl ... Name="userControl">
    <Grid Background="LightBlue">
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <TextBlock Text="Title" FontSize="30" Margin="10,0,0,0"/>
        <ContentPresenter Content="{Binding AdditionalContent, ElementName=userControl}" />
    </Grid>
</UserControl>

现在,您可以按如下方式使用它:

<local:UserControl1>
    <local:UserControl1.AdditionalContent>
        <Button>Click me</Button>
    </local:UserControl1.AdditionalContent>
</local:UserControl1>

点击我

您必须设置
控制模板

<UserControl>
<UserControl.Resources>
    <Style TargetType="{x:Type local:UserControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:UserControl1}">
                    <Grid Background="LightBlue">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="50" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Text="Title" FontSize="30" Margin="10,0,0,0"/>
                        <ContentPresenter Grid.Row="1" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>
</UserControl>

您可以对用户控件进行模板化,以添加额外的视觉效果,如
文本块

<UserControl>
<UserControl.Style>
  <Style TargetType="{x:Type UserControl}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate>              
          <Grid Background="LightBlue"> 
          <Grid.RowDefinitions> 
            <RowDefinition Height="50" /> 
            <RowDefinition Height="*" /> 
          </Grid.RowDefinitions> 
          <TextBlock Text="Title" FontSize="30" Margin="10,0,0,0"/> 
          <ContentPresenter Grid.Row="1" Content="{TemplateBinding Content}"  />
          </Grid> 
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</UserControl.Style>
<Button>
  Click me!
</Button>
</UserControl>

点击我!
将模板与

而不是使用内容演示器

因此,请放置以下位置:

<UserControl.Style>
        <Style TargetType="{x:Type UserControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type UserControl}" >
                          <Grid Background="LightBlue"> 
                           <Grid.RowDefinitions> 
                            <RowDefinition Height="50" /> 
                            <RowDefinition Height="*" /> 
                          </Grid.RowDefinitions> 
                           <TextBlock Text="Title" FontSize="30" Margin="10,0,0,0"/> 

                        <ContentControl  Grid.Row="1" Content="{TemplateBinding Content}"  />

                        </Grid> 
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Style>


对于您的用户控件

为什么要使用附加的依赖属性,而不仅仅是替换控件模板并绑定到内容属性?我同意您的看法。我刚发布了我的答案,就看到了你的答案。我投票赞成你的观点,对它为什么不起作用的解释很好,但我发现@blindmeis的答案更有用。你不必使用
,只需在类上使用
[ContentProperty(“AdditionalContent”)]
属性,然后就可以在控件的内容中使用自定义控件。这很好,因为没有人可以通过意外设置内容而不是
附加内容来破坏它。什么是
校准点
?intellisence找不到该类的可能解决方案,是自定义的吗?[[EDIT]]很抱歉,我没有检查方法
寄存器
参数。我认为应该将其更改为
UserControl1
进行编译。您是否有理由通过
UserControl.Resources/Style
设置它,而不是将
ControlTemplate
作为
UserControl.Template
的内容?不幸的是,似乎不支持设置
UserControl
Template
属性:UserControl.Template适合我。我使用TargetType=“{x:Type UserControl}”。感谢您的帮助。设置控件模板将禁止在您设置的任何内容中使用x:Name。在.NET4.7WPF应用程序中测试。名称范围显然已设置。请将模板与ContentControl一起使用,而不是使用ContentPresenter-为什么?最好在模板内使用ContentPresenter,请参阅使用
使其在资源字典中工作
<UserControl.Style>
        <Style TargetType="{x:Type UserControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type UserControl}" >
                          <Grid Background="LightBlue"> 
                           <Grid.RowDefinitions> 
                            <RowDefinition Height="50" /> 
                            <RowDefinition Height="*" /> 
                          </Grid.RowDefinitions> 
                           <TextBlock Text="Title" FontSize="30" Margin="10,0,0,0"/> 

                        <ContentControl  Grid.Row="1" Content="{TemplateBinding Content}"  />

                        </Grid> 
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Style>