2012-03-01 14 views
3

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(またはバインド可能なもの)を使用する方法はありますか?あなたはあなたではなく、添付プロパティのために行くことができ

+0

[**限り、あなたのクラスがDependencyObjectから派生して**、あなたがDependencyProperty識別子であなたの財産をバックアップするので、その依存関係プロパティにするオプションがあります。](http://msdn.microsoft .com/en-us/library/ms753358.aspx) –

+0

@jberger DependencyObjectから自分のクラスを派生させた場合、どうすればMarkupExtensionとして動作させることができますか? –

+0

私はあなたができるとは思わない。あなたのパターンを変更する必要があります。 Simonの答えの修正版を試すことができます。 –

答えて

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}" /> 
+0

私はあなたのソリューションを試しましたが、デザイナーでNullRefExceptionが発生します。 System.NullReferenceException オブジェクト参照がオブジェクトのインスタンスに設定されていません。 MS.Internal.Design.Markup.MarkupExtensionParser.ParseNamedArguments(TypeNode型、List'1引数) –

関連する問題