3

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 (または同様にバインド可能なもの) を使用する方法はありますか?

ヒントをありがとう

4

1 に答える 1

0

代わりに添付プロパティを使用することもできます。私が見ているように、それが唯一のオプションです。

例えば

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}" />
于 2012-03-01T16:36:25.273 に答える