什么是SilverO 3.0等效的BasedOn =“{StaticResource {x:Type TextBlock}}”

时间:2010-01-28 21:05:36

标签: c# wpf silverlight silverlight-3.0

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

如何在Silverlight中翻译BasedOn =“{StaticResource {x:Type TextBlock}}”。  那些人实现了这个目标吗?

谢谢。

4 个答案:

答案 0 :(得分:5)

Silverlight中没有相应的特定用法。 Silverlight仅支持用于访问Resources的字符串键。因此,使用{x:Type SomeType}作为密钥不起作用。

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

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

答案 1 :(得分:3)

我认为它可以向后退一点,你可以制作你的基本风格

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您现在可以在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中进行测试