有没有人有一个带有单个ContentPresenter的UserControl的简单示例?

dev*_*xer 6 wpf xaml user-controls contentpresenter

到目前为止,我有这个:

<UserControl
    x:Class="MyConcept.ExpanderPanel"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <Border
            Style="{StaticResource Border_PanelStyle}"
            CornerRadius="3" />
        <ContentPresenter />
    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

此UserControl的示例用法:

<nc:ExpanderPanel
    Grid.Row="0">
    <Expander
        IsExpanded="True"
        Header="NMT Users">
        <StackPanel>
            ...
        </StackPanel>
    </Expander>
</nc:ExpanderPanel>
Run Code Online (Sandbox Code Playgroud)

讨论

如果我跑这个,我什么也看不见.没有内容,甚至没有内置到UserControl的边框.

我想也许我需要创建ContentPresenter一个依赖属性,但我无法弄清楚如何将属性链接到UserControl的XAML中的ContentPresenter.

有人可以提供一个简单的例子,展示如何UserControl使用单个构建(或某种自定义控件)ContentPresenter吗?

Mua*_*Dib 4

ContentPresenters 主要在 ControlTemplates 中使用,并通过 TemplateBinding 绑定到 ContentControl.Content。来自此站点...使用 ContentPresenter 的按钮的控件模板

<Style TargetType="{x:Type Button}">
  <Setter Property="Background" Value="White" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate>
        <Grid>
          <Rectangle Fill="{TemplateBinding Property=Background}" />
            <ContentPresenter
              Content="{TemplateBinding Property=ContentControl.Content}" />
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)