Search code examples
silverlightsilverlight-toolkit

Silverlight Toolkit Themes: Exception on Setting Theme with TreeView


I've run into a problem trying to programmatically apply a Silverlight Toolkit Theme. An exception is raised upon setting the theme, under certain conditions.

XAML:

<StackPanel>
    <Button HorizontalAlignment="Left"
            Content="Press To Set Theme"
            Click="Button_Click" />
    <sdk:TreeView>
        <sdk:TreeViewItem Header="Items">
            <TextBlock Text="Item" />
        </sdk:TreeViewItem>
    </sdk:TreeView>
</StackPanel>

Code Behind:

private void Button_Click(object sender, RoutedEventArgs e)
{
    TwilightBlueTheme.SetIsApplicationTheme(Application.Current, true);
}

If I start it, then expand the TreeView, and then click the button, I get this:

System.ArgumentException: Value does not fall within the expected range.
    at MS.Internal.XcpImports.CheckHResult(UInt32 hr)

EDIT:

I've now tried a different way of doing this, and it still breaks with the same error. I wrapped the xaml in a Theme, like so:

<toolkit:Theme x:Name="ThemeContainer"
               ThemeUri="/System.Windows.Controls.Theming.TwilightBlue;component/Theme.xaml">
...
</toolkit:Theme>

And then changed the theme-switching to:

Theme themeContainer = (Theme)((FrameworkElement)Application.Current.RootVisual).FindName("ThemeContainer");

themeContainer.ThemeUri = new Uri("/System.Windows.Controls.Theming.ExpressionDark;component/Theme.xaml"), UriKind.RelativeOrAbsolute);

Same situation: it works, except if I've already expanded the TreeView then it breaks with the same error.

SL4 w/ April 2010


Solution

  • Well, it's seems that this is a bug from SL toolkit. I get this error if set the theme when the tree is expanded:

    System.Windows.Markup.XamlParseException: Failed to assign to property 'System.Windows.Controls.ContentControl.Content'.

    Anyway, the usual way of using themes works flawless:

    <theme:TwilightBlueTheme>
        ...
    </theme:TwilightBlueTheme>
    

    This is the theme namespace definition:

    xmlns:theme="clr-namespace:System.Windows.Controls.Theming;assembly=System.Windows.Controls.Theming.TwilightBlue"