0

是否可以这样做?如何基于Silverlight工具包主题创建样式?

在我的XAML代码,我有一些组合框与风格,这样的定义:(:ExpressionDark如)正确适用于每一个控件除了在这里,我定义的那些

<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的主题像上面那样的风格。

据我所知,在WPF中,我们可以使用x:使用“BasedOn”属性在silverlight主题上创建我们的Style对象。但是,似乎Silverlight 4无法这样做。

有关如何解决此问题的任何想法?

谢谢!

回答

1

将您的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> 
+0

感谢您的建议!但是,某些Style可以被其他控件重用,因此我没有声明它们是内联的( – Mathieu 2011-04-13 13:27:21

+0

)。您可以将DataTemplate定义为可重用的资源,请参阅我的编辑。 – BenCr 2011-04-13 14:08:27

+0

如何将它应用于不是在控件模板等项目控件中?例如,我想删除按钮的样式,如何才能使用该按钮的模板? – Lance 2011-09-20 07:41:41

0

删除样式中键:

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

基本上这意味着你上面所做的样式适用于组合框的目标类型,但也不是没有设置它的样式,因此命名为每一个组合框将默认继承这个。

UPDATE: 正如你可以看到所有的3种风格可以在同一个资源共存,无论你使用一个命名样式将被应用到上述控制,但在过去的三年组合框,所有3将没有钥匙的风格。这是如何在成像中完成的,就像来自MS的JetPack皮肤一样。

希望这有助于。

+0

感谢您的快速安装!不幸的是,我有多个ComboBox,其中一些必须使用不同的ComboBox风格,所以我不能删除x:Key属性。 – Mathieu 2011-04-13 13:19:45

+0

Uhm yes其实我让我解释一下,你可以使用我用连词与你命名样式:D我会修改我的回答 – 2011-04-13 14:09:07

+0

感谢您的解释。但是,我的问题是ExpressionDark Silverlight主题是不适用于具有Style =“{...}”设置的组合框。我正在寻找解决此问题的方法:/ – Mathieu 2011-04-13 14:22:03

0

嗨,这不是完全使用basedOn,但通过使用转换器,我们可以实现基于自定义风格的主题的目标。以下是我们如何做到的。

  1. 公开一个主题并将其放置在可将其绑定到样式的位置。

  2. 创建一个值转换器来转换您正在使用的样式。此转换器将返回基于主题的样式,所以这里是一个片段。

    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; 
    } 
    
  3. 绑定主题,风格和使用转换器,您使用

    风格每个自定义风格=“{结合主题,转换器= {StaticResource的styleConverter},ConverterParameter = {StaticResource的ButtonStyle1}} “

其中theme是Theme(System.Windows.Controls.Theming)类型的属性。

我将我的示例项目升空here 示例代码未更新,但您可以从此处开始。

相关问题