DataTemplate location problem

Topics: Conventions, Getting Started
Jul 15, 2011 at 9:06 AM

Hello,

i try to achive to load a data template that is only identified by type.
here is a example:

 

<UserControl x:Class="Admiral.ASWOnlineBase.DeviceMonitor.Views.TestView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:cal="http://www.caliburnproject.org">
    <UserControl.Resources>
        <DataTemplate DataType="{x:Type Customer}">
            <StackPanel cal:View.Model="{Binding}">
                <TextBlock x:Name="FirstName" />
                <TextBlock x:Name="LastName" />
            </StackPanel>
        </DataTemplate>
    </UserControl.Resources>
    <Grid>
        <ContentControl x:Name="PropertyNameToBind" />
    </Grid>
</UserControl>

 

The PropertyNameToBind contains an instance of type Customer.
The problem is, that the Convention searches for a View (CustomerView) instead of the DataTemplate.

If i would write

<ContentControl Content="{Binding PropertyNameToBind}" /> 

wpf would find the DataTemplate by itself.

Why i ask this: i do not want to write {Binding} expressions because CM can do this for me and i can not put the data template into the ContentControl.ContentTemplate node by name or inline.

Do i have extend the CM Framework to get this to work or do i something wrong?

Thanks, Bernhard

 

Coordinator
Jul 15, 2011 at 1:23 PM

CM can't do what you are asking it to do. You need to just put the binding expression in the ContentControl in this case.

Jul 15, 2011 at 1:23 PM
Edited Jul 15, 2011 at 1:26 PM

i found a point where the decision is done which BindableProperty has to be taken.

 

var useViewModel = element.ContentTemplate == null && element.ContentTemplateSelector == null;

if (useViewModel)
{
    return View.ModelProperty;
}
else
{
    return ContentControl.ContentProperty;
}

 

in my case, i would need the ContentControl.ContentProperty, but because i do not set the ContentTemplate and do not use a ContentTemplateSelector, the decision is wrong here.

i would need a way to influence this decision by applying a AttachedDependencyProperty or a AdHoc method that will be called when the binding with the model occurres.

(Sadly, cal:View.ApplyConventions="False" do not work in this case, because it only influence the view and not the content control itself...)

Can someone help me with this please?

Coordinator
Jul 15, 2011 at 1:33 PM

It would be less work and less code to just add the manual binding in this one case :) My recommendation would be to do that and move on.

Jul 15, 2011 at 1:48 PM

ok, i found a way. its maybe not the best solution, but it works for my needs:

<UserControl x:Class="Admiral.ASWOnlineBase.DeviceMonitor.Views.TestView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:cal="http://www.caliburnproject.org">
    <UserControl.Resources>
        <DataTemplate x:Key="CommonDataTemplate">
            <ContentPresenter Content="{Binding}" />
        </DataTemplate>
        <DataTemplate DataType="{x:Type Customer}">
            <StackPanel cal:View.Model="{Binding}">
                <TextBlock x:Name="FirstName" />
                <TextBlock x:Name="LastName" />
            </StackPanel>
        </DataTemplate>
    </UserControl.Resources>
    <Grid>
        <ContentControl x:Name="PropertyNameToBind" ContentTemplate="{DynamicResource CommonDataTemplate}" />
    </Grid>
</UserControl>

This would result into two content controls instead of one, but yes... i have to do it :)
Maybe i will later introduce my own attached property to handle this or create a intelligent data template that handles this issue better.

thanks for CM^^

Jul 18, 2011 at 9:50 AM

Hi,

i found a way that is economic enought to live with:

I use the DataTemplateSelector object from wpf to get the functionality that plays well with your implementation.
maybe you include this into your documentation as well:

<UserControl x:Class="Admiral.ASWOnlineBase.DeviceMonitor.Views.TestView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:cal="http://www.caliburnproject.org">
    <UserControl.Resources>
        <DataTemplateSelector x:Key="GenericDataTemplateSelector" />
        <DataTemplate DataType="{x:Type Customer}">
            <StackPanel cal:View.Model="{Binding}">
                <TextBlock x:Name="FirstName" />
                <TextBlock x:Name="LastName" />
            </StackPanel>
        </DataTemplate>
    </UserControl.Resources>
    <Grid>
        <ContentControl x:Name="PropertyNameToBind" ContentTemplateSelector="{DynamicResource GenericDataTemplateSelector}" />
    </Grid>
</UserControl>

Best regards,
Bernhard