【问题标题】:What is the Silverlight 3.0 equivalent for BasedOn="{StaticResource {x:Type TextBlock}}"BasedOn="{StaticResource {x:Type TextBlock}}" 的 Silverlight 3.0 等效项是什么
【发布时间】:2010-01-28 21:05:35
【问题描述】:

我正在尝试扩展 TextBlock 的基本样式。在 WPF 世界中简单的思考,在 Silverlight 中应该是一样的。但我在 x:Type 上遇到错误。

如何在 Silverlight 中翻译 BasedOn="{StaticResource {x:Type TextBlock}}"。 有谁做到了这一点?

谢谢。

【问题讨论】:

  • 您现在实际上可以在 Silverlight 5 中执行此操作。请参阅下面的答案

标签: c# wpf silverlight silverlight-3.0


【解决方案1】:

在 Silverlight 中没有与该特定用法等效的方法。 Silverlight 仅支持用于访问资源的字符串键。因此,使用{x:Type SomeType} 作为键不起作用。

在 Silverlight 中,您需要制作控件样式的完整副本。您可以通过使用具有执行此操作的工具的 Blend 或从 Silverlight 文档中复制和粘贴来执行此操作。 Control Styles and Templates

当然,一旦您拥有初始样式的副本,您就可以修改您的副本或创建其他样式,将此副本分配给 BasedOn 以创建一组变体。

【讨论】:

  • 嗨,安东尼,我怕有人会这么说,那就没事干然后去上班吧:(
【解决方案2】:

我认为稍微倒退一点会起作用,你可以制作你的基本风格

<Style TargetType="Button" x:Key="MyButtonStyle">
    <Setter Property="PropertyName" Value="PropertyValue" />
</Style>

那么您可以将所有按钮都基于该样式

<Style TargetType="Button" BasedOn="{StaticResource MyButtonStyle}" />

如果您需要添加到该样式,您可以将其基于命名样式

<Style TargetType="Button" BasedOn="{StaticResource MyButtonStyle}">
    <Setter Property="PropertyName" Value="PropertyValue" />
</Style>

<Style TargetType="Button" BasedOn="{StaticResource MyButtonStyle}" x:Key="MyOtherButtonStyle">
    <Setter Property="PropertyName" Value="PropertyValue" />
</Style>

【讨论】:

    【解决方案3】:

    应该是(和according to Jesse Liberty
    BasedOn="{StaticResource TextBlock}"

    【讨论】:

    • 嗨 Muad'Dib,我得到“无法找到具有名称/密钥 TextBlock 的资源”女巫实际上不是真的,因为我显然使用的是 silverlight 工具包中的 TwilightBlueTheme。
    【解决方案4】:

    您现在实际上可以在 Silverlight 5 中执行此操作。

    首先,声明你的风格

    <Style x:Key="TextBoxStyle" TargetType="TextBox" BasedOn="{local:Type TypeName=TextBox}">
    
    </Style>
    

    接下来,您需要创建一个在 WPF 和 Silverlight 5 中都可以使用的 MarkupExtension 来替换 x:Type

    /// A MarkupExtension which introduces x:Type like syntax to both WPF and Silverlight (Cross-platform). This is used internally
    /// for the themes, but is also useful e.g. when creating custom Control Templates for SciChart
    /// </summary>
    /// <remarks>
    /// Licensed under the CodeProject Open License
    /// http://www.codeproject.com/Articles/305932/Static-and-Type-markup-extensions-for-Silverlight
    /// </remarks>
    /// 
    public class TypeExtension : MarkupExtension
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TypeExtension" /> class.
        /// </summary>
        public TypeExtension()
        {
        }
    
        /// <summary>
        /// Initializes a new instance of the <see cref="TypeExtension" /> class.
        /// </summary>
        /// <param name="type">The type to wrap</param>
        public TypeExtension(Type type)
        {
            Type = type;
        }
    
        /// <summary>
        /// Gets or sets the type information for this extension.
        /// </summary>
        public System.Type Type { get; set; }
    
        /// <summary>
        /// Gets or sets the type name represented by this markup extension.
        /// </summary>
        public String TypeName { get; set; }
    
        public override Object ProvideValue(IServiceProvider serviceProvider)
        {
            if (Type == null)
            {
                if (String.IsNullOrWhiteSpace(TypeName)) throw new InvalidOperationException("No TypeName or Type specified.");
                if (serviceProvider == null) return DependencyProperty.UnsetValue;
    
                IXamlTypeResolver resolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
                if (resolver == null) return DependencyProperty.UnsetValue;
    
                Type = resolver.Resolve(TypeName);
            }
            return Type;
        }
    }
    

    经测试可在 WPF 和 Silverlight 中使用

    【讨论】:

      猜你喜欢
      • 2011-07-08
      • 2011-01-10
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      相关资源
      最近更新 更多