Include controls in a ControlTemplate from another .xaml

737 Views Asked by At

I want to create a control which displays an image and a watermark (image or something else) on it.
But the watermark should be loaded from another XAML file in order to let people custom the way the watermark will appear : alignment, opacity, size, nature of the watermark (TextBlock, Image, ...).

For instance I could load my watermark with this appearance

<Border BorderThickness="5" BorderBrush="Aqua" Width="50" Height="50">
  <Image Source="play.png" />
</Border>


This code is from my Themes/generic.xaml and MyWatermarkControl (inherits from Control) is the class which contain the code of the control (dependency properties).

<Style TargetType="local:MyWatermarkControl">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="local:MyWatermarkControl">
        <Grid>
          <Image Source="{TemplateBinding ImagePath}" />
          <Image x:name="watermark" Source="play.png" /> <!--I want this to be loaded from another .xaml-->
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

My search results lead me to add in my ControlTemplate stuff like ContentPresenter, ContentTemplate, DataTemplate : so many results and I cannot understand how do they work but the fact they are nested

1

There are 1 best solutions below

0
On

You can add a Source property to your MyWatermarkControl then bind the Source property of your embedded Image to this property. For more details see the following tutorial that I wrote:

A Simple Pattern for Creating Re-useable UserControls in WPF / Silverlight