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が更新されていないようです。
'DataContext'を変更したときに、どうなると思いますか? – Jon