Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Bind To Data within a Datatemplate of a ContentControl

I have the following simplified Example:

    <Window x:Class="TemplateBinding.MainWindow"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             Title="MainWindow" Height="350" Width="525">         <Window.Resources>             <ResourceDictionary>                 <ResourceDictionary.MergedDictionaries>                     <ResourceDictionary                             Source="pack://application:,,,/TemplateBinding;component/PersonTemplate.xaml" />                 </ResourceDictionary.MergedDictionaries>             </ResourceDictionary>         </Window.Resources>         <Grid>             <ContentControl ContentTemplate="{StaticResource PersonTemplate}" />         </Grid>     </Window> 

With:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">          <DataTemplate x:Key="PersonTemplate">             <Border Width="100" Height="100" Background="RosyBrown">                 <TextBlock Text="{Binding Path=FirstName}" VerticalAlignment="Center" TextAlignment="Center"/>             </Border>         </DataTemplate>      </ResourceDictionary> 

as my DataTemplate in a separate ResourceDictionary file.

I set my DataContext in the Construcor of my MainWindow and have verified it by just displaying the first name like this: <ContentControl Grid.Row="1" Content="{Binding FirstName}"/>.

In an other scenario wher I use a DataTemplate with a ListBox I do the Binding exactly the same way in my DataTemplate and it just works.

I know that the DataTemplate is working except the binding because it correctly shows the size and background color.

What am I doing wrong? How would the Binding in my DataTemplate have to look?

like image 731
markus s Avatar asked Mar 13 '13 15:03

markus s


People also ask

How data binding happens explain with an example?

Data binding can also mean that if an outer representation of the data in an element changes, then the underlying data can be automatically updated to reflect the change. For example, if the user edits the value in a TextBox element, the underlying data value is automatically updated to reflect that change.

What is DataTemplate WPF?

DataTemplate is about the presentation of data and is one of the many features provided by the WPF styling and templating model. For an introduction of the WPF styling and templating model, such as how to use a Style to set properties on controls, see the Styling and Templating topic.

Where do you put DataTemplate?

According to Microsofts App Studio the DataTemplates should live in a DataTemplates Subdirectory under the Views Directory.

What is a data template?

The data template is the method by which you communicate your request for data to the data engine. It is an XML document whose elements collectively define how the data engine will process the template to generate the XML. The data engine supports the following functionality: Single and multiple data queries.


1 Answers

You need to bind the Content property of the ContentControl

<ContentControl Content="{Binding}" ContentTemplate="{StaticResource PersonTemplate}" /> 

This will set the DataContext of the ContentControl as Content of the control.

Setting only the ContentTemplate property is not enough. The ContentControl does not implicitly use its DataContext as Content.

like image 67
Jehof Avatar answered Sep 29 '22 18:09

Jehof