简体   繁体   中英

Include controls in a ControlTemplate from another .xaml

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM