代码之家  ›  专栏  ›  技术社区  ›  Calin

BasedOn=“{StaticResource{x:Type TextBlock}”的Silverlight 3.0等价物是什么

  •  5
  • Calin  · 技术社区  · 15 年前

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

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

    4 回复  |  直到 15 年前
        1
  •  5
  •   AnthonyWJones    15 年前

    {x:Type SomeType} 因为钥匙不起作用。

    在Silverlight中,您需要制作控件样式的完整副本。您可以通过使用Blend来实现这一点,Blend有用于实现这一点的工具,或者从Silverlight文档中复制粘贴它。 Control Styles and Templates

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

        2
  •  3
  •   Timbot    13 年前

    我认为稍微后退一点会有用的,你可以做你的基本风格

    <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
  •  0
  •   Luke Woodward    15 年前

    according to Jesse Liberty )
    BasedOn="{StaticResource TextBlock}"

        4
  •  0
  •   Dr. Andrew Burnett-Thompson    10 年前

    您现在可以在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中工作

    推荐文章