如何将样式基于Silverlight工具箱主题?

| 是否有可能做到这一点? 在我的xaml代码中,我有一些具有样式的ComboBoxes,其定义如下:
<Style x:Key=\"comboProjectsStyle\"
       TargetType=\"ComboBox\">
    <Setter Property=\"ItemTemplate\">
        <Setter.Value>
            <DataTemplate>
                <Grid>
                    <TextBlock Text=\"{Binding Path=Name}\"
                               FontSize=\"14\" />
                </Grid>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key=\"comboDataSourcesStyle\"
       TargetType=\"ComboBox\">
    <Setter Property=\"ItemTemplate\">
        <Setter.Value>
            <DataTemplate>
                <Grid>
                    <TextBlock Text=\"{Binding Path=DescriptiveName}\"
                               FontSize=\"14\" />
                </Grid>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>
<ComboBox Width=\"300\"
          Style=\"{StaticResource comboProjectsStyle}\" />
<ComboBox Width=\"300\"
          Style=\"{StaticResource comboDataSourcesStyle}\" />
Silverlight主题(例如:ExpressionDark)可以正确地应用于除上面定义了样式的控件之外的所有控件。 据我了解,在WPF中,我们可以使用\“ BasedOn \”属性基于x:Style作为Silverlight主题的样式。但是,似乎无法使用Silverlight 4做到这一点。 关于如何解决这个问题的任何想法? 谢谢!     
已邀请:
将ItemTemplate声明为资源而不是样式,然后将应用主题样式。
<UserControl xmlns:sdk=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk\" 
    xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"
    xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"
    xmlns:d=\"http://schemas.microsoft.com/expression/blend/2008\"
    xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" 
    x:Class=\"Silverlight_Spike.MainPage\"
    mc:Ignorable=\"d\"
    d:DesignHeight=\"300\" d:DesignWidth=\"400\">
    <UserControl.Resources>
        <DataTemplate x:Key=\"DataTemplate1\">
            <Grid>
                <TextBlock Text=\"{Binding Name}\" FontSize=\"14\" />
            </Grid>
        </DataTemplate>
    </UserControl.Resources>

    <ComboBox ItemTemplate=\"{StaticResource DataTemplate1}\" />

</UserControl>
    
删除样式中的键:
    <Style TargetType=\"ComboBox\">
                <Setter Property=\"ItemTemplate\">
                    <Setter.Value>
                        <DataTemplate>
                            <Grid>
                                <TextBlock Text=\"{Binding Path=Name}\" FontSize=\"14\" />
                            </Grid>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
        </Style>
    <Style x:Key=\"comboProjectsStyle\" TargetType=\"ComboBox\">
            <Setter Property=\"ItemTemplate\">
                <Setter.Value>
                    <DataTemplate>
                        <Grid>
                            <TextBlock Text=\"{Binding Path=Name}\" FontSize=\"14\" />
                        </Grid>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
    </Style>

    <Style x:Key=\"comboDataSourcesStyle\" TargetType=\"ComboBox\">
            <Setter Property=\"ItemTemplate\">
                <Setter.Value>
                    <DataTemplate>
                        <Grid>
                            <TextBlock Text=\"{Binding Path=DescriptiveName}\" FontSize=\"14\" />
                        </Grid>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
    </Style>

    <ComboBox Width=\"300\" Style=\"{StaticResource comboProjectsStyle}\" />
    <ComboBox Width=\"300\" Style=\"{StaticResource comboDataSourcesStyle}\" />

<ComboBox Width=\"300\" />
<ComboBox Width=\"300\" />
<ComboBox Width=\"300\" />
基本上,这意味着您在上面创建的样式适用于ComboBox的目标类型,因此没有命名,因此每个未设置样式的Combobox都将继承此样式。 更新: 如您所见,所有3种样式都可以共存于同一资源中,每当您使用命名样式时,它将被应用到所述控件,但是对于最后三个组合框,所有3种样式都将具有没有键的样式。就像在MS的JetPack皮肤中一样,这是在贴边过程中完成的。 希望这可以帮助。     
嗨,这不是完全使用basedOn,而是通过使用转换器,我们可以实现将自定义样式基于主题的目标。这是我们的操作方式。 公开一个主题并将其放置在可以将其绑定到样式的位置。 创建一个值转换器以转换您正在使用的样式。该转换器将返回基于主题的样式,因此这是一个代码段。
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    if (parameter is Style)
    {
        Style retStyle = parameter as Style;
        Theme themeContainer;
        if (value is Theme)
            themeContainer = value as Theme; //(App.Current as App).AppTheme;
        else
            themeContainer = (App.Current as App).AppTheme; 

        if (themeContainer != null)
        {
            foreach (DictionaryEntry i in themeContainer.ThemeResources)
            {
                if (i.Value is Style)
                {
                    Style t = i.Value as Style;
                    if (t.TargetType == retStyle.TargetType)
                    {
                        Style newStyle = new Style();
                        newStyle.TargetType = retStyle.TargetType;
                        newStyle.BasedOn = t;

                        foreach (Setter set in retStyle.Setters)
                            newStyle.Setters.Add(new Setter() { Property = set.Property, Value = set.Value }); 

                        return newStyle;                                              
                    }
                }
            }
        }
        return retStyle;
    }
    return null; 
}
将主题绑定到样式,并在您使用的每种自定义样式上使用转换器 样式= \“ {绑定主题,转换器= {StaticResource styleConverter},ConverterParameter = {StaticResource ButtonStyle1}} \” 其中theme是Theme(System.Windows.Controls.Theming)类型的属性。 我在这里赞扬了我的示例项目 示例代码未更新,但是您可以从此处开始。     

要回复问题请先登录注册