Set ContentTemplate in CodeBehind: XamlParseException 2260 Error

Posted by user362215 on Stack Overflow See other posts from Stack Overflow or by user362215
Published on 2010-06-09T08:51:38Z Indexed on 2010/06/09 8:52 UTC
Read the original article Hit count: 286

Hi, I'd like to change the ContentTemplate of a ContentPresenter in the CodeBehind file. But if I run the Silverlight 4 application a XamlParseException with the error code 2260 occures.

foreach (ContentPresenter item in Headers)
{
    item.ContentTemplate = Parent.UnselectedHeaderTemplate;
}

if ((index >= 0) && (index < Headers.Count))
{
    ContentPresenter item0 = (ContentPresenter)Headers[index];
    item0.ContentTemplate = Parent.SelectedHeaderTemplate;
}

If I do only the foreach code without the code in the "if", it works. And if I only do the code in the "if" without the foreach it works too. But togheter (the "if"-code and the foreach-code) it doesn't work.

I have no idea why it doesn't work. The two templates look like this:

<Setter Property="UnselectedHeaderTemplate">
        <Setter.Value>
            <DataTemplate>
                <ContentControl Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"
                           Margin="10,-10"
                           FontSize="72"
                           Foreground="#FF999999"
                           CacheMode="BitmapCache"/>

            </DataTemplate>
        </Setter.Value>
    </Setter>

    <!-- SelectedHeader template -->
    <Setter Property="SelectedHeaderTemplate">
        <Setter.Value>
            <DataTemplate>
                <ContentControl Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"
                           Margin="10,-10"
                           FontSize="72" 
                           Foreground="{TemplateBinding Foreground}"
                           CacheMode="BitmapCache"/>
            </DataTemplate>
        </Setter.Value>
    </Setter>

If you have an idea what problem is please tell me.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about silverlight-4.0