我有一个 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
接口(interface)。我总是忘记这一点。
关于c# - TextBlock 不更新内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37416668/