c# - Silverlight-从部分中删除隐式样式

原文 标签 c# silverlight xaml styles silverlight-toolkit

Silverlight - Removing implicit styles from a section

I'm using Silverlight toolkit to set the styling for my whole application. Now I need to remove the implicit styling from a specific section, so that it doesn't interfere with other styling in that section.

Basically I want to do something like this:

<theme:BureauBlackTheme>
    <StackPanel x:Name="LayoutRoot">
        <StackPanel><!-- Use the standard Bureau Black theme here --></StackPanel>
        <StackPanel><!-- I don't want any implicit styling on this section --></StackPanel>
    </StackPanel>
</theme:BureauBlackTheme>

Looking at the source code of the Silverlight toolkit, I found that themes are applied by merging resource dictionaries:

...
// Load the theme
ResourceDictionary resources = null;
using (stream)
{
    resources = ResourceParser.Parse(stream, true);
    owner.MergedDictionaries.Add(resources);
}
...

Where the theme files contain a bunch of implicit styles:

<!--ScrollBar-->
<Style  TargetType="ScrollBar">
    <Setter Property="MinWidth" Value="17" />
    ...

Therefore, I need a way to remove all the implicit styles from a specific section, but only from that section. The reason I need this, is because these styles are interfering with the styling of a third party control (I think this has to do with the precedence of the control styles).

Is this possible in Silverlight 4? Workarounds are welcome too.

Thanks in advance!

Answer

The best you could do is to short circuit the implicit Styles added by the SL Toolkit. For example, if you add an empty style like so:

<theme:BureauBlackTheme>
    <StackPanel x:Name="LayoutRoot">
        <StackPanel><!-- Use the standard Bureau Black theme here --></StackPanel>
        <StackPanel>
            <!-- I don't want any implicit styling on this section -->
            <StackPanel.Resources>
                <Style TargetType="ScrollBar" />
            </StackPanel.Resources>
        </StackPanel>
    </StackPanel>
</theme:BureauBlackTheme>

Then the empty Style will prevent the theme's Style from being applied, as only 1 implicit Style can be applied at a time. You'd have to do this for each element type supported by the SL Toolkit though.

翻译

我正在使用Silverlight toolkit设置整个应用程序的样式。现在,我需要从特定部分中删除隐式样式,以免干扰该部分中的其他样式。

基本上我想做这样的事情:

<theme:BureauBlackTheme>
    <StackPanel x:Name="LayoutRoot">
        <StackPanel><!-- Use the standard Bureau Black theme here --></StackPanel>
        <StackPanel><!-- I don't want any implicit styling on this section --></StackPanel>
    </StackPanel>
</theme:BureauBlackTheme>


通过查看Silverlight工具箱的源代码,我发现主题是通过合并资源字典来应用的:

...
// Load the theme
ResourceDictionary resources = null;
using (stream)
{
    resources = ResourceParser.Parse(stream, true);
    owner.MergedDictionaries.Add(resources);
}
...


主题文件包含一堆隐式样式:

<!--ScrollBar-->
<Style  TargetType="ScrollBar">
    <Setter Property="MinWidth" Value="17" />
    ...


因此,我需要一种方法来删除特定部分中的所有隐式样式,但只能从该部分中删除。我之所以需要它,是因为这些样式会干扰第三方控件的样式(我认为这与precedence of the control styles有关)。

Silverlight 4有可能吗?也欢迎变通方法。

提前致谢!
最佳答案
最好的办法是使SL Toolkit添加的隐式样式短路。例如,如果您添加一个空样式,如下所示:

<theme:BureauBlackTheme>
    <StackPanel x:Name="LayoutRoot">
        <StackPanel><!-- Use the standard Bureau Black theme here --></StackPanel>
        <StackPanel>
            <!-- I don't want any implicit styling on this section -->
            <StackPanel.Resources>
                <Style TargetType="ScrollBar" />
            </StackPanel.Resources>
        </StackPanel>
    </StackPanel>
</theme:BureauBlackTheme>


然后,空的样式将阻止应用主题的样式,因为一次只能应用1个隐式样式。不过,您必须为SL工具包支持的每种元素类型执行此操作。
相关推荐

c# - 如何将BoundField转换为HyperLinkField?

c# - 如何获得父级AppDomain?

c# - 枚举值的生成常数

c# - 关于python正则表达式的两个问题

c# - 连接来自C#中两个数据库的表

c# - C#委托具有不同可选参数的方法

c# - 使用货币时的最佳做法

c# - LINQ to XML C#获取根元素属性

c# - C#网络凭据未传递到服务器?

c# - CPU使用率问题