Note that there are some explanatory texts on larger screens.

plurals
  1. POBind DataTemplate to CustomControl problem
    primarykey
    data
    text
    <p>What I've got is a custom control, which implements datatemplate in xaml:</p> <pre><code>&lt;DataTemplate x:Key="Templat"&gt; &lt;StackPanel&gt; &lt;TextBlock Text="Sample Text" /&gt; &lt;TextBlock Text="{Binding Surname}" /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; </code></pre> <p>and custom control:</p> <pre><code> &lt;local:MyControl x:Name="MyControl1" ItemTemplate="{StaticResource Templat}" Margin="0,0,-24,8"/&gt; </code></pre> <p>generic.xaml (in my custom control library) has:</p> <pre><code>&lt;ControlTemplate TargetType="local:MyControl"&gt; &lt;Canvas Name="LayoutRoot" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" Background="{TemplateBinding Background}" CacheMode="BitmapCache"&gt; &lt;Canvas Name="ItemsHost" Margin="10,185,0,0" Height="615" Width="{TemplateBinding Width}" CacheMode="BitmapCache"&gt; &lt;local:CustomItem x:Name="Item1" ContentTemplate="{TemplateBinding ItemTemplate}" /&gt; &lt;local:CustomItem x:Name="Item2" ContentTemplate="{TemplateBinding ItemTemplate}" /&gt; &lt;/Canvas&gt; &lt;/Canvas&gt; &lt;/ControlTemplate&gt; </code></pre> <p>What am I doing wrong? </p> <p>I created a custom item control, which has a few custom content controls inside. I want them all to have the same content template, so I bound their content template to defined itemtemplate in parent control. </p> <p>My problem is that controls will show textblock with "Sample Text" text, but not the one with the binded value. I was trying to specify DataContext in codebehind (like <code>DataContext = new Person() { Surname="Johnson" }</code> or via xaml. None of them worked. </p> <p>The DataContext (Person class) looks like was passed correct, but passed DataTemplate misses the <code>'{Binding Surname}'</code> expression. Have you got any ideas what can be wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload