我试图扩展一个TextBlock的基础样式。在WPF中是很简单的事情,应该在Silverlight中也一样。但是我遇到了一个关于x:Type的错误。
如何在Silverlight中将BasedOn="{StaticResource {x:Type TextBlock}}"进行翻译。 有没有人做到了这一点?
谢谢。
我试图扩展一个TextBlock的基础样式。在WPF中是很简单的事情,应该在Silverlight中也一样。但是我遇到了一个关于x:Type的错误。
如何在Silverlight中将BasedOn="{StaticResource {x:Type TextBlock}}"进行翻译。 有没有人做到了这一点?
谢谢。
{x:Type SomeType}
作为键是不起作用的。<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>
现在在Silverlight 5中,您实际上可以这样做。
首先,声明您的样式
<Style x:Key="TextBoxStyle" TargetType="TextBox" BasedOn="{local:Type TypeName=TextBox}">
</Style>
/// 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 中测试可用
应该是(并且根据Jesse Liberty的说法)BasedOn="{StaticResource TextBlock}"