2010-01-28 61 views
5

我想扩展TextBlock的基础样式。在WPF世界中简单思考,在Silverlight中应该是一样的。但是我在x:Type上遇到错误。什么是BasedOn的Silverlight 3.0等效=“{StaticResource {x:Type TextBlock}}”

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

谢谢。

+0

你现在可以在Silverlight 5中做到这一点。看到我的回答下面 – 2015-06-06 10:17:56

回答

5

没有一个相当于在Silverlight该特定的使用情况。 Silverlight仅支持访问资源的字符串键。因此,使用{x:Type SomeType}作为关键不起作用。

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

当然,一旦你的初始样式的副本然后或者可以修改,复制或建立其他样式分配这个副本支持算法FMP创建一组变化。

+0

嗨安东尼, 我怕有人会说这个,那么没事做就去上班:( – Calin 2010-01-29 06:27:17

0

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

+0

嗨Muad'Dib, 我得到“无法找到资源的名称/关键TextBlock”女巫实际上是不真实的,因为我清楚地使用TwilightBlueTheme从silverlight工具包。 – Calin 2010-01-29 06:26:36

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

实际上,你可以做到这一点,现在在Silverlight 5

首先,声明你的风格

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

</Style> 

接下来,你需要创建两个WPF和Silverlight 5的作品更换的MarkupExtension X:类型

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

相关问题