[英]TextBlock doesn't update content
我有一個TextBlock,我想寫入文本。 為此,我在后台使用TextBox和其他一些邏輯。
<TextBlock
Text="{Binding Chunk}"
TextBlock.FontFamily="Arial"
VerticalAlignment="Center"
HorizontalAlignment="Left" />
<TextBox
Text="{Binding Chunk, UpdateSourceTrigger=PropertyChanged}"
Width="400"
AcceptsReturn="True"
MaxHeight="80"
Margin="0,10,20,10"
Padding="0,5,0,5" />
public string Chunk
{
get { return chunk; }
set
{
if (value == chunk || (value != null && value.Length > 400))
return;
chunk = value;
OnPropertyChanged("Chunk");
}
}
現在,如果我在TextBox中寫入內容,TextBlock的內容將按預期更新。 但是如果我從背景中向Chunk寫一些內容,TextBlock和TextBox的內容都不會更新。 觸發了OnPropertyChanged事件,但顯然會被忽略。
知道為什么嗎?
您需要在ViewModel上實現INotifyPropertyChanged
接口。 我一直都忘記這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.