Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The DataContext of a DataTemplate is set to the object that it is bound to. So in your case the DataContext for your Templates are <code>Product_Type</code> and you are expecting it to be <code>MyViewModel</code>. </p> <p>There is a workaround for what you need. It uses a <a href="http://msdn.microsoft.com/en-us/library/system.windows.data.binding.relativesource%28v=vs.110%29.aspx" rel="nofollow"><code>RelativeSource</code></a> binding and <a href="http://msdn.microsoft.com/en-us/library/system.windows.data.relativesourcemode%28v=vs.110%29.aspx" rel="nofollow"><code>FindAncester</code></a> to access the DataContext of the Parent object.</p> <p>Your DataTemplate bindings should look like this:</p> <p><strong>XAML</strong></p> <pre><code> &lt;DataTemplate x:Key="T1"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="40" /&gt; &lt;RowDefinition Height="40" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="120" /&gt; &lt;ColumnDefinition Width="*" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TextBlock Grid.Column="0" Grid.Row="0" Text="Music" Style="{StaticResource ResourceKey=textBlockStyle}" /&gt; &lt;TextBox Grid.Column="1" Grid.Row="0" Style="{StaticResource ResourceKey=textBoxStyle}" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentControl}}, Path=DataContext.MusicName}" /&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="T2"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="40" /&gt; &lt;RowDefinition Height="40" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="120" /&gt; &lt;ColumnDefinition Width="*" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TextBlock Grid.Column="0" Grid.Row="0" Text="Currency" Style="{StaticResource ResourceKey=textBlockStyle}" /&gt; &lt;ComboBox Grid.Column="1" Grid.Row="0" Style="{StaticResource ResourceKey=comboBoxStyle}" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentControl}}, Path=DataContext.Currency_List}" SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentControl}}, Path=DataContext.Currency}" /&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; </code></pre> <p><strong>From MSDN</strong> </p> <blockquote> <p><em>Find Ancestor</em> - Refers to the ancestor in the parent chain of the data-bound element. You can use this to bind to an ancestor of a specific type or its subclasses.</p> </blockquote> <p>The <code>AncestorType</code> attribute goes up the visual tree and finds the first Ancestor of the given type, in this case it's looking for <code>ContentControl</code> the path to the required property can then be set relative to this. </p> <p><strong>Update</strong></p> <p>Think of a template as a guide on how to display an object. The DataContext of the DataTemplate is going to be whatever object it is asked it to display. </p> <p>In this case the ContentControl is told to display <code>Product_Type</code> and, depending on the value of <code>Product_Type</code>, to use a particular Template. <code>Product_Type</code> is given to the DataTemplate and becomes the DataContext.</p> <p><a href="http://wpftutorial.net/DataTemplates.html" rel="nofollow">WPFTutorials</a> has some good examples. </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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