Note that there are some explanatory texts on larger screens.

plurals
  1. POSilverlight: Binding a custom control to an arbitrary object
    text
    copied!<p>I am planning on writing a hierarchical organizational control, similar to an org chart. Several org chart implementations are out there, but not quite fit what I have in mind.</p> <p>Binding fields in a <code>DataTemplate</code> to a custom object does not seem to work.</p> <p>I started with a generic, custom control, i.e.</p> <pre><code>public class NodeBodyBlock : ContentControl { public NodeBodyBlock() { this.DefaultStyleKey = typeof(NodeBodyBlock); } } </code></pre> <p>It has a simple style in <code>generic.xaml</code>:</p> <pre><code> &lt;Style TargetType="org:NodeBodyBlock"&gt; &lt;Setter Property="Width" Value="200" /&gt; &lt;Setter Property="Height" Value="100" /&gt; &lt;Setter Property="Background" Value="Lavender" /&gt; &lt;Setter Property="FontSize" Value="11" /&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="org:NodeBodyBlock"&gt; &lt;Border Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Background="{TemplateBinding Background}" CornerRadius="4" BorderBrush="Black" BorderThickness="1" &gt; &lt;Grid&gt; &lt;VisualStateManager/&gt; ... clipped for brevity &lt;/VisualStateManager.VisualStateGroups&gt; &lt;ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /&gt; &lt;/Grid&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>My plan now is to be able to use this common definition as a base definition of sorts, with customized version of it used to display different types of content.</p> <p>A simple example would be to use this on a user control with the following style:</p> <pre><code>&lt;Style TargetType="org:NodeBodyBlock" x:Key="TOCNode2"&gt; &lt;Setter Property="ContentTemplate"&gt; &lt;Setter.Value&gt; &lt;DataTemplate&gt; &lt;StackPanel&gt; &lt;TextBlock Text="{Binding Path=NodeTitle}"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>and an instance defined as </p> <pre><code>&lt;org:NodeBodyBlock Style="{StaticResource TOCNode2}" x:Name="stTest" DataContext="{StaticResource DummyData}" /&gt; </code></pre> <p>The <code>DummyData</code> is defined as </p> <pre><code>&lt;toc:Node NodeNumber="mynum" NodeStatus="A" NodeTitle="INLine Node Title!" x:Key="DummyData"/&gt; </code></pre> <p>With a simple C# class behind it, where each of the fields is a public property.</p> <p>When running the app, the Dummy Data values simply do not show up in the GUI. A trivial test such as </p> <pre><code>&lt;TextBlock Text="{Binding NodeTitle}" DataContext="{StaticResource DummyData}"/&gt; </code></pre> <p>works just fine.</p> <p>Any ideas around where I am missing the plot?</p> <p>Update: Binding to the datacontext in the definition in <code>generic.xaml</code> works fine, but any binding in the <code>ContentPresenter</code> is lost.</p>
 

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