快速浏览Silverlight3 Beta: SystemColor(系统颜色)
中国IT站 www.chinaitz.com 2009-5-8

   现在,除了默认的Button模版不依赖于Foreground 和 Background 之外,活干的差不多了。

  它可以使用更多的颜色和一个BorderBrush,但也有一些颜色在Button的属性上是无效的。具我所知也就能修改这两种属性.

   我需要另外一个Button版本,其使用Foreground, Background 和 BorderBrush. 那我要如何组合这个style呢?

   我想到可以使用BasedOn style,其XAML如下;

<UserControl.Resources>
     <local:MySystemStyles
         x:Key="mySystemStyles" />
     <Style
         x:Key="buttonStyle"
         BasedOn="{Binding Source={StaticResource mySystemStyles},Path=ButtonStyle}">

   在运行时无法工作( BasedOn 绑定时IE报错).

   也许可以变换一下思路,当它在运行时创建一个style时,我可以通知 MySystemStyles 类。它应该基于当前的style。看起来有些麻烦但却可行;

public class MySystemStyles
{
  public Style BasedOnButtonStyle
  {
    get
    {
      return (basedOnButtonStyle);
    }
    set
    {
      basedOnButtonStyle = value;
    }
  }   
  public Style ButtonStyle
  {
    get
    {
      if (buttonStyle == null)
      {
        buttonStyle = MakeStyle(typeof(Button), basedOnButtonStyle, buttonPropertyValues);
      }
      return (buttonStyle);
    }
  }
  static Style MakeStyle(Type t, Style basedOnStyle, Dictionary&lt;DependencyProperty, object&gt; propertyValues)
  {
    Style style = new Style(t);

    style.BasedOn = basedOnStyle;

    foreach (var item in propertyValues)
    {
      style.Setters.Add(new Setter(item.Key, item.Value));
    }
    return (style);
  }
  static Dictionary&lt;DependencyProperty, object&gt; buttonPropertyValues =
    new Dictionary&lt;DependencyProperty, object&gt;()
    {
      { Control.ForegroundProperty, new SolidColorBrush(SystemColors.ControlTextColor) },
      { Control.BackgroundProperty, new SolidColorBrush(SystemColors.ControlColor) },
      { Button.BorderBrushProperty, new SolidColorBrush(SystemColors.ActiveBorderColor) }
    };

  Style buttonStyle;
  Style basedOnButtonStyle;
}

共6篇上一页 1 2 3 4 5 6 下一页
责任编辑:admin本文仅代表作者观点,与中国IT站立场无关。
收藏】 【推荐】 【投稿】 【 】 【打印】 【关闭
评论加载中...