6

到目前为止,我有这个:

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

此 UserControl 的示例用法:

<nc:ExpanderPanel
    Grid.Row="0">
    <Expander
        IsExpanded="True"
        Header="NMT Users">
        <StackPanel>
            ...
        </StackPanel>
    </Expander>
</nc:ExpanderPanel>

讨论

如果我运行它,我什么也看不到。没有显示任何内容,甚至没有显示在 UserControl 中的边框。

我想也许我需要创建ContentPresenter一个依赖属性,但我不知道如何将该属性链接到 UserControl 的 XAML 中的 ContentPresenter。

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

4

1 回答 1

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>
于 2009-10-19T01:38:39.590 回答