windows phone 7 – Silverlight ItemTemplate binding – Education Career Blog

I want to set an ItemTimplate according to some property.
I’ve just create a markup, where I set ItemTemplate like this:

<controls:Panorama Title="Some Title" ItemsSource="{Binding Modules}" ItemTemplate="{Binding Id, Converter={StaticResource ControlTemplateConverter}}">

        <ImageBrush ImageSource="PanoramaBackground.png"/>


And implement ControlTemplateConverter like this:

public class ControlTemplateConverter: IValueConverter
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        StringBuilder sbTemp = new StringBuilder();
        sbTemp.Append("<DataTemplate ");
        sbTemp.Appen("xmlns='' ");
        sbTemp.Append("xmlns:x='' ");
        sbTemp.Append("<TextBlock Text=\"News News\" />");
        return (DataTemplate)XamlReader.Load(sbTemp.ToString());

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        throw new NotImplementedException();

I registered a converter:

    <converter:ControlTemplateConverter x:Key="ControlTemplateConverter" />

But when I set a breakpoint in ControlTemplateConverter.Convert it never stops in debug.
ItemSource loads everytime.
What’s wrong?
Thank you.


I suspect that the binding fails to find the property Id in the same DataContext that the Modules property is found. Hence there is no call to the converter. This is probably because Id is actually a property of each Module and somehow you imagine that the ItemTemplate binding gets resolved for each item. It doesn’t. You have one template that gets applied to all items in the ItemsSource.

Leave a Comment