What is the Silverlight 3.0 equivalent for BasedOn="{StaticResource {x:Type TextBlock}}"
Asked Answered
N

4

5

I am trying to extend a base style for a TextBlock. Simple think in WPF world, should be the same in Silverlight. But I get a error on x:Type.

How can I translate BasedOn="{StaticResource {x:Type TextBlock}}" in Silverlight. Anyone out there who achieved this ?

Thank you.

Necroscopy answered 28/1, 2010 at 21:5 Comment(1)
You can actually do this now in Silverlight 5. See my answer belowAttenuation
A
5

There isn't an equivalent to that particular usage in Silverlight. Silverlight only supports string keys for accessing Resources. Hence the use of {x:Type SomeType} as a key doesn't work.

In Silverlight you need to make a complete copy of the controls style. You can do this either by using Blend which has tools for doing this or by copy'n'pasting it from the Silverlight documentation. Control Styles and Templates

Of course once you have a copy of the initial style you can then either modify your copy or create other Styles assigning this copy to BasedOn to create a set of variations.

Aitch answered 28/1, 2010 at 23:39 Comment(1)
Hi Anthony, I was afraid someone will say this, well nothing to do then to get to work then :(Necroscopy
G
3

I Think it will work to go a little backwards, you can make your base style

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

then you can base all buttons on that style

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

Then if you need to add to that style you can just base it on the named style

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

or

<Style TargetType="Button" BasedOn="{StaticResource MyButtonStyle}" x:Key="MyOtherButtonStyle">
    <Setter Property="PropertyName" Value="PropertyValue" />
</Style>
German answered 19/4, 2012 at 20:12 Comment(0)
I
0

should ought to be (and according to Jesse Liberty)
BasedOn="{StaticResource TextBlock}"

Inapprehensive answered 28/1, 2010 at 21:9 Comment(1)
Hi Muad'Dib, I get "Cannot find a Resource with the Name/Key TextBlock" witch is actually not true because I am clearly using the TwilightBlueTheme from the silverlight toolkit.Necroscopy
A
0

You can actually do this now in Silverlight 5.

First, declare your style

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

</Style>

Next, you need to create a MarkupExtension which works in both WPF and Silverlight 5 to replace 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;
    }
}

Tested as working in WPF and Silverlight

Attenuation answered 6/6, 2015 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.