有没有人有一个带有单个

有没有人有一个带有单个

本文介绍了有没有人有一个带有单个 ContentPresenter 的 UserControl 的简单示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我有这个:

<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 中内置的边框.

If I run this, I see nothing. No content is presented, not even the border that is built into the UserControl.

我想也许我需要将 ContentPresenter 设为依赖属性,但我不知道如何将该属性链接到 UserControl 的 XAML 中的 ContentPresenter.

I thought maybe I needed to make the ContentPresenter a dependency property, but I couldn't figure out how I would link the property to the ContentPresenter in the UserControl's XAML.

谁能提供一个简单的例子来展示如何使用单个 ContentPresenter 构建 UserControl(或某种自定义控件)?

Can someone provide a simple example that shows how to build a UserControl (or some kind of custom control) with a single ContentPresenter?

推荐答案

ContentPresenters 主要用于 ControlTemplates 并通过 TemplateBinding 绑定到 ContentControl.Content.来自这个 网站...一个使用按钮的控件模板内容展示者

ContentPresenters are main used in ControlTemplates and bound with a TemplateBinding to the ContentControl.Content.from this site... a control template for a button that uses a 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>

这篇关于有没有人有一个带有单个 ContentPresenter 的 UserControl 的简单示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 13:15