WPF MVVM textBox文本绑定

Nic*_*ams 5 c# wpf mvvm inotifypropertychanged

我刚刚开始使用MVVM,如果我做了一些非常愚蠢的事情,那么道歉.我试着写一个非常简单的测试来看看我是否能记住一切,而对于我的生活,我不明白为什么它不起作用.

在我看来,我有一个textBox,其text属性绑定到ViewModel中的值.然后当按下按钮时,应该更改值并更新textBox.

我可以看到值确实改变了(我在buttom press命令中添加了MessageBox.Show()行)但是textBox没有更新.

我认为这意味着我没有正确实施INotifyPropertyChanged事件,但我无法看到我的错误.

有人能指出我正确的方向吗?

这是代码:

视图

<Window x:Class="Mvvm.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">

<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
    <TextBox Height="40" Width="200" Text="{Binding helloWorld.Message, UpdateSourceTrigger=PropertyChanged}"/>
    <Button Command="{Binding UpdateTimeCommand}">Update</Button>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

在视图后面

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel.MainWindowViewModel();
    }
}
Run Code Online (Sandbox Code Playgroud)

视图模型

namespace Mvvm.ViewModel
{
internal class MainWindowViewModel
{
    private HelloWorld _helloWorld;

    /// <summary>
    /// Creates a new instance of the ViewModel Class
    /// </summary>
    public MainWindowViewModel()
    {
        _helloWorld = new HelloWorld("The time is " + DateTime.Now.ToString("HH:mm:ss"));
        UpdateTimeCommand = new Commands.UpdateTimeCommand(this);
    }

    /// <summary>
    /// Gets the HellowWorld instance
    /// </summary>
    public HelloWorld helloWorld
    {
        get
        {
            return _helloWorld;
        }
        set
        {
            _helloWorld = value;
        }
    }

    /// <summary>
    /// Updates the time shown in the helloWorld 
    /// </summary>
    public void UpdateTime()
    {
        helloWorld = new HelloWorld("The time is " + DateTime.Now.ToString("HH:mm:ss"));
    }

    public ICommand UpdateTimeCommand
    {
        get;
        private set;
    }
}
Run Code Online (Sandbox Code Playgroud)

模型

namespace Mvvm.Model
{
    class HelloWorld : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public HelloWorld(string helloWorldMessage)
        {
            Message = "Hello World! " + helloWorldMessage;
        }

        private string _Message;
        public string Message
        {
            get
            {
                return _Message;
            }
            set
            {
                _Message = value;
                OnPropertyChanged("Message");
            }
        }

        private void OnPropertyChanged(string p)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(p));
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

命令

namespace Mvvm.Commands
{
    internal class UpdateTimeCommand : ICommand
    {
        private ViewModel.MainWindowViewModel _viewModel;
        public UpdateTimeCommand(ViewModel.MainWindowViewModel viewModel)
        {
            _viewModel = viewModel;
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            _viewModel.UpdateTime();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

对不起,这么长的帖子,这是我的错误发布的地方,但我看了很久,我不知道我做错了什么

谢谢!

Agu*_*les 6

您遇到的问题是您正在更改错误的属性.HelloWorld.Message您正在更改MainWindowViewModel.HelloWorld属性,而不是更改属性.如果更改此行,您的代码将正常工作:

public void UpdateTime()
{
    helloWorld = new HelloWorld("The time is " + DateTime.Now.ToString("HH:mm:ss"));
}
Run Code Online (Sandbox Code Playgroud)

对于这个

public void UpdateTime()
{
    helloWorld.Message = "The time is " + DateTime.Now.ToString("HH:mm:ss");
}
Run Code Online (Sandbox Code Playgroud)

如果要保留原始代码,则需要为ViewModel实现INotifyPropertyChanged,并在更改helloWorld对象时启动事件.

希望这可以帮助