WPFLocalizationExtension ( CodePlex で利用可能) を使用して、WPF アプリケーションで文字列をローカライズしています。この単純な MarkupExtension は、次のような単純なシナリオでうまく機能します。
<Button Content="{lex:LocText MyApp:Resources:buttonTitle}" />
しかし、次のようなもっと複雑なことを試すとすぐに行き詰まりました。
<Window Title="{lex:LocText MyApp:Resources:windowTitle, FormatSegment1={Binding Version}}" />
(リソース付きwindowTitle = "MyApp v{0}"
)。
FormatSegment1 はプレーンな INotifyPropertyChange プロパティであるため、何もバインドできません。FormatSegment1 が DependencyProperty であれば可能だったので、ソースをダウンロードしてパッチを当ててみました。
修正しました
[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));
// ...
}
クラスはBaseLocalizeExtension
から継承しMarkupExtension
ます:
public abstract class BaseLocalizeExtension<TValue> : MarkupExtension, IWeakEventListener, INotifyPropertyChanged
ビルドすると、通常の"GetValue/SetValue does not exist in current context"
エラーが発生します。BaseLocalizeExtension
クラスを継承させようとしましDependencyObject
たが、大量のエラーが発生しました。
MarkupExtension で xaml バインド可能な DependencyProperty (または同様にバインド可能なもの) を使用する方法はありますか?
ヒントをありがとう