3

I'm using WPFLocalizationExtension (Available on CodePlex) to localize strings in my WPF application. This simple MarkupExtension works well in simple scenarios like this:

<Button Content="{lex:LocText MyApp:Resources:buttonTitle}" />

but I got stuck as soon as I tried some more complicated things like:

<Window Title="{lex:LocText MyApp:Resources:windowTitle, FormatSegment1={Binding Version}}" />

(With resource windowTitle = "MyApp v{0}").

Since FormatSegment1 is a plain INotifyPropertyChange property I can't bind anything to it. It would be possible if FormatSegment1 was a DependencyProperty, so I downloaded the sources and tried to patch'em.

I modified

[MarkupExtensionReturnType(typeof(string))]
public class LocTextExtension : BaseLocalizeExtension<string>
{

    // ---- OLD property

    //public string FormatSegment1
    //{
    //    get { return this.formatSegments[0]; }
    //    set
    //    {
    //        this.formatSegments[0] = value;
    //        this.HandleNewValue();
    //    }
    //}

    // ---- NEW DependencyProperty

    /// <summary>
    /// The <see cref="FormatSegment1" /> dependency property's name.
    /// </summary>
    public const string FormatSegment1PropertyName = "FormatSegment1";

    /// <summary>
    /// Gets or sets the value of the <see cref="FormatSegment1" />
    /// property. This is a dependency property.
    /// </summary>
    public string FormatSegment1
    {
        get
        {
            return (string)GetValue(FormatSegment1Property);
        }
        set
        {
            SetValue(FormatSegment1Property, value);
        }
    }

    /// <summary>
    /// Identifies the <see cref="FormatSegment1" /> dependency property.
    /// </summary>
    public static readonly DependencyProperty FormatSegment1Property = DependencyProperty.Register(
        FormatSegment1PropertyName,
        typeof(string),
        typeof(LocTextExtension),
        new UIPropertyMetadata(null));

    // ...
}

The BaseLocalizeExtension class inherits from MarkupExtension:

public abstract class BaseLocalizeExtension<TValue> : MarkupExtension, IWeakEventListener, INotifyPropertyChanged

When I build, I get the usual "GetValue/SetValue does not exist in current context" error. I try to make BaseLocalizeExtension class inherit from DependencyObject but I get tons of errors.

Is there a way to use a xaml bindable DependencyProperty (or something that is bindable as well) in a MarkupExtension?

Thank you for you hints

xmedeko
  • 7,336
  • 6
  • 55
  • 85
silentman.it
  • 361
  • 5
  • 18
  • [**so long as your class derives from DependencyObject**, you have the option to back your property with a DependencyProperty identifier and thus to make it a dependency property.](http://msdn.microsoft.com/en-us/library/ms753358.aspx) – Jake Berger Mar 01 '12 at 16:39
  • @jberger if I derive my class from DependencyObject, how do I make it behave as a MarkupExtension? – silentman.it Mar 02 '12 at 09:37
  • i don't think you can. you'll have to change your pattern. you could try a modified version of Simon's answer. – Jake Berger Mar 02 '12 at 14:48

1 Answers1

0

You could go for an attached property instead, it's your only option as I see it.

e.g.

public static readonly DependencyProperty FormatSegment1Property = DependencyProperty.RegisterAttached(
        "FormatSegment1", typeof(string), typeof(LocTextExtension), new PropertyMetadata(default(string)));

public static void SetFormatSegment1(DependencyObject element, string value)
{
    element.SetValue(FormatSegment1Property, value);
}

public static string GetFormatSegment1(DependencyObject element)
{
    return (string)element.GetValue(FormatSegment1Property);
}

.

<Window Title="{lex:LocText MyApp:Resources:windowTitle}" lex:LocText.FormatSegment1="{Binding Version}" />
Terkel
  • 1,575
  • 8
  • 9
  • I tried your solution and I get a NullRefException in the designer: System.NullReferenceException Object reference not set to an instance of an object. at MS.Internal.Design.Markup.MarkupExtensionParser.ParseNamedArguments(TypeNode type, List`1 arguments) – silentman.it Mar 02 '12 at 08:43