Wpf:文本块在更改后不更新

Wpf: The text block doesn't update after change

我有一个使用 MVVM 的 Wpf 应用程序,代码如下所示:

XAML:

<StackPanel Orientation="Vertical">
    <TextBox Text="{Binding DataFolder}" TextWrapping="Wrap"/>
    <Button Content="Convert" Padding="8" Command="{Binding ConvertCommand}" IsEnabled="True" MinWidth="220"/>
    <TextBlock Text="{Binding DoneMessage, UpdateSourceTrigger=PropertyChanged}"></TextBlock>
</StackPanel>

视图模型:

public class ConverterViewModel : NotificationObject
{
    public string DataFolder { get; set; }
    public string DoneMessage { get; set; }
    public DelegateCommand ConvertCommand { get; set; }

    private readonly List<BaseConverter> _converters = new List<BaseConverter>
    {
        new VisualCheckEventConverter()
    };

    public ConverterViewModel()
    {
        ConvertCommand = new DelegateCommand(VisualCheckEventConvertCommandExecute);
        DataFolder = ConfigurationManager.AppSettings["InputFolder"];
        DoneMessage = "Not done yet.";
    }

    private void VisualCheckEventConvertCommandExecute()
    {
        foreach (var c in _converters)
            c.Convert(DataFolder);
        DoneMessage = "Done!";
    }
}

当我运行应用程序时,显示消息"Not done yet.",但执行命令后文本块的文本没有更新为"Done!"。

如何让它发挥作用?

如果您希望视图得到通知,您需要在 DoneMessage 属性 setter 中 notifypropertychanged
此外,AFAIK 将 UpdateSourceTrigger=PropertyChanged 放在 TextBlock 上是没有意义的,因为它是只读的。如果您希望在文本更改时通知您的 ViewModel,您应该将它放在 TextBox 上。
应该是这样的:

    private string _doneMessage;

    public string DoneMessage
    {
        get { return _doneMessage; }
        set
        {
            _doneMessage = value;
            //the method name may vary based on the implementation of INotifyPropertyChanged
            NotifyPropertyChanged("DoneMessage");
        }
    }