Silverlight 4.0 从代码隐藏创建样式

Silverlight 4.0 从代码隐藏创建样式,silverlight-4.0,Silverlight 4.0,在我的silverlight应用程序中,我想从代码隐藏创建一个样式,并将创建的样式应用于控件。在数据库中,我们有一个表,它存储一个值,如fontname、color、Bold、Italic、Font Size。我想根据该参数创建一个样式,并应用于控件 他们有可能吗 如果可能,请添加代码 提前谢谢 试试这样的方法: // sampleText is the control to apply the style to Style styl = new Style(sample

在我的silverlight应用程序中,我想从代码隐藏创建一个样式,并将创建的样式应用于控件。在数据库中,我们有一个表,它存储一个值,如fontname、color、Bold、Italic、Font Size。我想根据该参数创建一个样式,并应用于控件

他们有可能吗

如果可能,请添加代码


提前谢谢

试试这样的方法:

      // sampleText is the control to apply the style to
      Style styl = new Style(sampleText.GetType());
      switch (str)
      {
        case "Bold":
          styl.Setters.Add(new Setter(FontWeightProperty, FontWeights.ExtraBold));
          break;
        case "Italic":
          styl.Setters.Add(new Setter(FontStyleProperty, FontStyles.Italic));
          break;
      }
      sampleText.Style = styl;
以下是简单的示例项目: