Does anyone have a simple example of a UserControl with a single ContentPresenter?
Asked Answered
P

1

6

So far, I have this:

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

Sample usage of this UserControl:

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

Discussion

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

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.

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

Parka answered 19/10, 2009 at 1:24 Comment(1)
This looks like it will do the trick: blog.pixelingene.com/?p=24Parka
C
4

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>
Centrepiece answered 19/10, 2009 at 1:38 Comment(3)
It shows up now, but I'm not sure how this relates to my question. I'm trying to build a custom control or user control that contains a ContentPresenter, not set the ControlTemplate for a button.Parka
you have to use a ControlTemplate for your control, this is an example of how to do so. Basically, you have to tell the ContentControl HOW to display what you want it to display. It has a set of "default" templates for some built-in things. social.msdn.microsoft.com/forums/en-US/wpf/thread/…Meyerhof
@Muad'Dib...you're image should be a mouseTeachin

© 2022 - 2024 — McMap. All rights reserved.