silverlightxamlsilverlight-4.0themessilverlight-toolkit

How can I base a style on a Silverlight toolkit theme?


is it possible to do this?

In my xaml code, I have some ComboBoxes with a style, defined like this:

<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}" />

The silverlight theme (eg: ExpressionDark) is correctly applied on every controls except on the ones where I have defined a style, like above.

As I understand, in WPF we could use x:Style an base our Style on the silverlight theme using the "BasedOn" property. However, it seems like it is not possible to do so with Silverlight 4.

Any ideas on how to approach this?

Thanks!


Solution

  • Declare your ItemTemplate as a resource rather than in a style then your theme style will apply.

    <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>