WPFLocalizationExtension(CodePlexで利用可能)を使用して、WPFアプリケーションの文字列をローカライズしています。この単純なMarkupExtensionは、このような単純なシナリオに適しています:MarkupExtension:単純なプロパティをDependencyPropertyに変換する
<Button Content="{lex:LocText MyApp:Resources:buttonTitle}" />
が、私は次のようにいくつかのより複雑な事をしようとしたとして、私はすぐに捕まってしまった:(リソースwindowTitle = "MyApp v{0}"
で)
<Window Title="{lex:LocText MyApp:Resources:windowTitle, FormatSegment1={Binding Version}}" />
。
FormatSegment1は単純なINotifyPropertyChangeプロパティなので、何もバインドできません。 FormatSegment1がDependencyPropertyの場合、ソースをダウンロードしてpatch'emしようとしました。
私はBaseLocalizeExtension
クラスがMarkupExtension
から継承
[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));
// ...
}
を変更:
public abstract class BaseLocalizeExtension<TValue> : MarkupExtension, IWeakEventListener, INotifyPropertyChanged
私はビルドすると、私はいつも"GetValue/SetValue does not exist in current context"
エラーが発生します。 クラスをDependencyObject
から継承させようとしていますが、多大なエラーが発生します。
MarkupExtensionにxamlバインド可能なDependencyProperty(またはバインド可能なもの)を使用する方法はありますか?あなたはあなたではなく、添付プロパティのために行くことができ
[**限り、あなたのクラスがDependencyObjectから派生して**、あなたがDependencyProperty識別子であなたの財産をバックアップするので、その依存関係プロパティにするオプションがあります。](http://msdn.microsoft .com/en-us/library/ms753358.aspx) –
@jberger DependencyObjectから自分のクラスを派生させた場合、どうすればMarkupExtensionとして動作させることができますか? –
私はあなたができるとは思わない。あなたのパターンを変更する必要があります。 Simonの答えの修正版を試すことができます。 –