Silverlight工具包主题:使用TreeView设置主题的例外

时间:2011-08-31 04:55:45

标签: silverlight silverlight-toolkit

我在尝试以编程方式应用Silverlight Toolkit主题时遇到了问题。在某些条件下设置主题时会引发异常。

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>

代码背后:

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

如果我启动它,然后展开TreeView,然后单击按钮,我明白了:

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

编辑:

我现在尝试了另外一种方法,它仍然会出现同样的错误。我将xaml包装在一个主题中,如下所示:

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

然后将主题转换为:

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

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

相同的情况:它有效,除非我已经扩展了TreeView,然后它就会出现同样的错误。

SL4 w / 2010年4月

2 个答案:

答案 0 :(得分:0)

关于如何使用Silverlight Toolkit

动态更改主题,请查看this post

答案 1 :(得分:0)

嗯,这似乎是SL工具包中的一个错误。如果在扩展树时设置主题,我会收到此错误:

  

System.Windows.Markup.XamlParseException:无法分配给   property'System.Windows.Controls.ContentControl.Content'。

无论如何,使用主题的常用方式完美无瑕:

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

这是主题命名空间定义:

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