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