2016-11-19 19 views
0

テキストボックスとコンボボックスを使用してwpfユーザーコントロールを作成しました。私は確信していますwpfユーザーコントロール内のコントロールのプロパティにアクセスする際のエラー

<textboxunitconvertor:TextBoxUnitConvertor Name="wDValueControl" TextBoxText="{Binding _FlClass.SWa_SC.Value , RelativeSource={RelativeSource AncestorType=Window}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="161" Height="28" HorizontalAlignment="Left" VerticalAlignment="Top"/> 

: は、テキストボックスのテキストプロパティにアクセスするために私がコントロールを使用し、以下のようにテキストを結合している別のプロジェクトに以下のコード

public static readonly DependencyProperty TextBoxTextP = DependencyProperty.Register(
     "TextBoxText", typeof(string), typeof(TextBoxUnitConvertor)); 

public string TextBoxText 
{ 
    get { return txtValue.Text; } 
    set { txtValue.Text = value; } 
} 

を使用していましたバインドに使用されているクラスは正常に動作しています。なぜなら、私は自分のプロジェクトで直接テキストボックスを使ってビンするのですが、正しく動作しますが、usercontrolのtextboxのtextプロパティにバインドするとnullが返され、バインディングは機能しません。誰でも助けてくれますか?

答えて

0

あなたの依存関係プロパティの宣言が間違っています。これは、CLRプロパティラッパーのゲッターとセッターはGetValueメソッドとSetValueをメソッドを呼び出す場合は、以下のように見えるように持っている:あなたのユーザーコントロールのXAMLで

public static readonly DependencyProperty TextBoxTextProperty = 
    DependencyProperty.Register(
     "TextBoxText", typeof(string), typeof(TextBoxUnitConvertor)); 

public string TextBoxText 
{ 
    get { return (string)GetValue(TextBoxTextProperty); } 
    set { SetValue(TextBoxTextProperty, value); } 
} 

、あなたがこのようなプロパティにバインドします:

<TextBox Text="{Binding TextBoxText, 
    RelativeSource={RelativeSource AncestorType=UserControl}}" /> 

あなたはいつでもTextBoxTextプロパティの変更通知を取得する必要がある場合は、あなたがPropertyMetadataとPropertyChangedCallbackを登録することができは、Registerメソッドに渡さ:

public static readonly DependencyProperty TextBoxTextProperty = 
    DependencyProperty.Register(
     "TextBoxText", typeof(string), typeof(TextBoxUnitConvertor), 
     new PropertyMetadata(TextBoxTextPropertyChanged)); 

private static void TextBoxTextPropertyChanged(
    DependencyObject o, DependencyPropertyChangedEventArgs e) 
{ 
    TextBoxUnitConvertor t = (TextBoxUnitConvertor)o; 
    t.CurrentValue = ... 
} 
+0

おかげでたくさんの値にTextBoxTextで次に

public string TextBoxText { get { return (string)GetValue(TextBoxTextProperty); } set { SetValue(TextBoxTextProperty, value); } } public static readonly DependencyProperty TextBoxTextProperty = DependencyProperty.Register("TextBoxText", typeof(string), typeof(TextBoxUnitConvertor), new PropertyMetadata("")); 

:このコードを使用します。別の質問。作成されたプロパティ "TextBoxText"を以下のような別のプロパティにバインドする必要がある場合、どうすればいいのですか。プライベートdouble _CurrentValue; [ブラウズ可能(false)] public double CurrentValue { get { return _CurrentValue; } セット{_CurrentValue =値; } } – ali

+0

編集された答えに示すようにPropertyChangedCallbackを登録します。 – Clemens

+0

非常に良いです。しかし、もし2つの方法バインドineed、どのようにすることができますか?ユーザーコントロールのテキストボックスのテキストをユーザーが変更した場合、メインプロジェクトのバインドされたクラスが変更されたことを意味しますか? – ali

0

依存関係プロパティの権利を作成していません。カスタムコントロールバインドtxtValue.Text

関連する問題