Ramin Bateni
Ramin Bateni

Reputation: 17425

WPF write ControlTemplate for a ContentControl when its content type is UserControl

I have a ContentControl like this:

<ContentControl>
    <userControls:TestControl/>
</ContentControl>

OR like this [when i have PRISM system]:

<ContentControl prism:RegionManager.RegionName="TestView"/>

I see the final UserControl well until this step when i start the program.

In the above samples the Content type is UserControl. Now i want give a ControlTemplate to this ContentControl. Then i created a style named StyleTest and used it in my Xaml:

<ContentControl    Style="{StaticResource StyleTest}"> .....

My style:

<Style x:Key="StyleTest" TargetType="ContentControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Grid Margin="10">
                    <Border CornerRadius="10" BorderBrush="#ffffffff" BorderThickness="5">
                        <StackPanel>

                            <ContentPresenter Content="{Binding}"/>

                            <TextBlock>Some additional text to test template</TextBlock>

                        </StackPanel>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

But when i start the program the UserControl can not be seen and i just see this text and a border around it: Some additional text to test template

Upvotes: 3

Views: 4464

Answers (3)

Wouter
Wouter

Reputation: 2958

There are 3 ways to do this.

- by setting the ContentTemplate
- by setting the Template
- or using the Border directly and apply a style.

In this case I would use the Border and apply a style because it looks like the ContentControl is only used to do add a styled Border.

<StackPanel>
    <StackPanel.Resources>
        <Style x:Key="BorderStyle" TargetType="{x:Type Border}">
            <Setter Property="BorderBrush" Value="Red" />
            <Setter Property="BorderThickness" Value="5" />
            <Setter Property="CornerRadius" Value="10" />
            <Setter Property="Margin" Value="10" />
        </Style>
        <Style x:Key="ContentTemplateStyle" TargetType="{x:Type ContentControl}">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Border Style="{StaticResource BorderStyle}">
                            <!--  Bind to the DataContext of the DataTemplate which is the Content of the ContentControl  -->
                            <!-- <ContentPresenter Content="{Binding}" />-->
                            <!--  TemplateBinding improves performance  -->
                            <ContentPresenter Content="{TemplateBinding Content}" />
                            <!--  Using the TemplatedParent  -->
                            <!--<ContentPresenter Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"/>-->
                        </Border>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="ControlTemplateStyle" TargetType="{x:Type ContentControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ContentControl">
                        <Border Style="{StaticResource BorderStyle}">
                            <ContentPresenter />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </StackPanel.Resources>
    <ContentControl Style="{StaticResource ContentTemplateStyle}">
        <Button>ContentTemplateStyle</Button>
    </ContentControl>
    <ContentControl Style="{StaticResource ControlTemplateStyle}">
       <Button>ControlTemplateStyle</Button>
    </ContentControl>
    <Border Style="{StaticResource BorderStyle}">
        <Button>BorderStyle</Button>
    </Border>
</StackPanel>

Upvotes: 2

Ramin Bateni
Ramin Bateni

Reputation: 17425

I found my mistake place and changed this part of my codes:

<Style x:Key="StyleTest" TargetType="ContentControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>

To this one:

<Style x:Key="StyleTest" TargetType="ContentControl">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate >

When i play with Template its effects is on inner content of the ContentControl (in this question: template of the UserControl) But when i play with ContentTemplate, its effects is on ContentControl layout.

ContentPresenter tag is same and it works now...

<ContentPresenter Content="{Binding}"/>

Upvotes: 0

Wouter
Wouter

Reputation: 2958

datacontext is a type?. Normally it will be inherited in the visual tree so I think it should be left blank. Content is the content of the parent. Normally you can use a template binding or set the content source property.

but using this template will only show you a white border around content. The original template is lost. So you should provide the entire template for the control.

Now maybe the control contains margin and border properties and you can set those from your style and leave the original template in place.

Upvotes: 0

Related Questions