2010-12-08 13 views
1

WPFでカスタムコントロールを作成しましたが、正しくバインドできません。コードの中のプロパティの値を明示的に設定すると、すべてうまく動作します。ここでWPfバインディングの問題

は、私のコントロール内のTextBoxためのXAMLです:

<TextBox Name="txtText" Grid.Row="0" Grid.Column="0" IsReadOnly="True" Text="{Binding Text, Mode=OneWay}" /> 

し、次のように背後にあるコード内の関連するプロパティは次のとおりです。

public static readonly DependencyProperty TextConverterProperty = DependencyProperty.Register("TextConverter", typeof(IValueConverter), typeof(Selector)); 
public static readonly DependencyProperty EntityIdProperty = DependencyProperty.Register("EntityId", typeof(long), typeof(Selector)); 

public string Text 
{ 
    get 
    { 
     string result = this.EntityId.ToString(); 

     if (this.TextConverter != null) 
     { 
      result = this.TextConverter.Convert(result, null, null, null) as string; 
     } 

     return result; 
    } 
} 

public long EntityId 
{ 
    get 
    { 
     return (long)this.GetValue(EntityIdProperty); 
    } 
    set 
    { 
     this.SetValue(EntityIdProperty, value); 

     this.OnPropertyChanged("Text"); 
     this.OnPropertyChanged("EntityId"); 
    } 
} 

public IValueConverter TextConverter 
{ 
    get 
    { 
     return this.GetValue(TextConverterProperty) as IValueConverter; 
    } 
    set 
    { 
     this.SetValue(TextConverterProperty, value); 
    } 
} 

そして今、私のページにあるXAMLの実装:

<controls:Selector x:Name="txtReferringCase" EntityId="{Binding ReferringDACaseId}" TextConverter="{StaticResource daCaseNumberConverter}" Grid.Column="5" Grid.Row="0" Grid.ColumnSpan="3" ButtonClicked="txtReferringCase_ButtonClicked" /> 

ここでは奇妙な部分があります。私は、ページのためのDataContextを設定することができますし、何も起こりませんが、私はコメント行のコメントを解除する場合、テキストは問題なく私のユーザーコントロールに表示:

_caseScreen = new DACaseScreen(itemId); 
this.DataContext = _caseScreen; 
//this.txtReferringCase.EntityId = _caseScreen.ReferringDACaseId; 

編集:私は言及を忘れてしまったもうひとつ..私がブレークポイントを押して、コントロールのEntityIdとTextプロパティをチェックすると、私は期待していた値を両方表示します。 UIが更新されていないようです。

+0

'DataContext'を変更したときに、どうなると思いますか? – Jon

答えて

1

ゲッタとDependencyPropertiesのためのセッターを便宜上余分な振る舞いをしたい場合は、DependencyPropertyにあなたが望むことをする変更ハンドラを渡してください。

public static readonly DependencyProperty EntityIdProperty = DependencyProperty.Register("EntityId", typeof(long), typeof(Selector),new UIPropertyMetadata(EntityIdChanged)); 

public long EntityId 
{ 
    get 
    { 
     return (long)this.GetValue(EntityIdProperty); 
    } 
    set 
    { 
     this.SetValue(EntityIdProperty, value); 
    } 
} 

private static void EntityIdChanged(object sender, DependencyPropertyChangedEventArgs e) 
{ 
    var control = (Selector)sender; 
    control.OnPropertyChanged("Text"); 
} 

依存性プロパティーにはPropertyChangedを呼び出す必要はありません。また、UserControlのtxtTextバインディングを変更する必要がありました。ソースコードをDataContextではなくコントロール自体に設定する必要がありましたが、コード内のプロパティを設定しても問題ありませんでした。セレクタと呼ばれるコントロールが組み込まれていて、重要ではないので、コントロールの名前を変更することができます。

+0

パーフェクト。ありがとうございました! –

0

OnPropertyChangedコールを行うまで、あなたのUIは更新されません。 DataContextを設定しても起動しません。

public static readonly DependencyProperty TextConverterProperty = DependencyProperty.Register("TextConverter", typeof(IValueConverter), typeof(Selector)); 
public static readonly DependencyProperty EntityIdProperty = DependencyProperty.Register("EntityId", typeof(long), typeof(Selector)); 

しかし、あなたはテキスト依存、のみTextConverterと実体識別子を登録していけない。また

あなたが行います。つまり、あなたのOnPropertyChanged(?)は動作しません(それとも、ただ:)ここでは、その行を表示することを忘れなかった)XAMLは、彼らが唯一あり、それらを使用していないためだけGetValueSetValueを呼び出す必要があります

+0

**にバインドしないと、テキストは依存関係にはなりません。依存関係プロパティでなくても、 "Text"上でOnPropertyChangedを呼び出すことができます – Vitalik

関連する問題