简体   繁体   English

在另一个.xaml中的ControlTemplate中包含控件

[英]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, ...). 但是水印应该从另一个XAML文件中加载,以便让人们自定义水印的显示方式:对齐方式,不透明度,大小,水印的性质(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). 这段代码来自我的Themes / generic.xaml,而MyWatermarkControl (从Control继承)是包含控件代码(依赖属性)的类。

<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 我的搜索结果导致我添加了ControlTemplate之类的内容,例如ContentPresenter,ContentTemplate,DataTemplate:如此之多的结果,我无法理解它们如何工作,但实际上它们是嵌套的

You can add a Source property to your MyWatermarkControl then bind the Source property of your embedded Image to this property. 您可以将Source属性添加到MyWatermarkControl然后将嵌入式ImageSource属性绑定到此属性。 For more details see the following tutorial that I wrote: 有关更多详细信息,请参见我编写的以下教程:

A Simple Pattern for Creating Re-useable UserControls in WPF / Silverlight 在WPF / Silverlight中创建可重复使用的用户控件的简单模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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