简体   繁体   English

WPF绑定属性未在2向模式下更新

[英]Wpf binded property not updating in 2-way-mode

I have simple gallery with notes and text box to add new one by title. 我有一个带有注释和文本框的简单画廊,可以按标题添加一个新的画廊。 I use mvvm, other bindings work correcty. 我使用mvvm,其他绑定正常工作。 When I set title of note and click add property NewNote.Title is updated and object saved in database. 当我设置注释的标题并单击添加属性NewNote.Title将被更新并将对象保存在数据库中。 After that I clear NewNote.Title but UI is not updated. 之后,我清除NewNote.Title,但未更新UI。

here is short video https://youtu.be/l3vFwI-a4TQ 这是短视频https://youtu.be/l3vFwI-a4TQ

xaml a

<TextBox Text="{Binding NewNote.Title}" />

page view model 页面浏览模型

class NotesPageViewModel : INotifyPropertyChanged
{
    public ObservableCollection<NoteViewModel> Notes { get; set; }
    public NoteViewModel NewNote { get; set; }

    public NotesPageViewModel()
    {
        NewNote = new NoteViewModel();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    internal void LoadNotes()
    {
        using (var uow = new UnitOfWork())
        {
            Notes.Clear();
            uow.NotesRepository.OrderBy(n => n.Position).ToList()
                .ForEach(note => Notes.Add((NoteViewModel)note));
        }
    }

    internal void AddNote()
    {
        if (string.IsNullOrWhiteSpace(NewNote.Title))
            return;

        using (var uow = new UnitOfWork())
        {
            uow.NotesRepository.Add((Note)NewNote);
            uow.Complete();
        }
        NewNote.Title = "";
        LoadNotes();
    }
}

object view model 对象视图模型

class NoteViewModel : INotifyPropertyChanged
{
    public int Id { get; set; }
    public string Title { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

You're not calling the OnPropertyChanged on the viewmodel when you're setting the property 设置属性时,您没有在视图模型上调用OnPropertyChanged

class NoteViewModel : INotifyPropertyChanged
{
    private int _id;

    public int Id
    {
        get { return _id; }
        set
        {
            _id = value;
            OnPropertyChanged("Id");
        }
    }

    private string _title;

    public string Title
    {
        get { return _title; }
        set
        {
            _title = value;
            OnPropertyChanged("Title");
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Also, the LoadNotes and AddNotes should be called via a command, and the new title should be sent as a parameter. 另外,应通过命令调用LoadNotes和AddNotes,并将新标题作为参数发送。 https://msdn.microsoft.com/en-us/library/ms752308(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/ms752308(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM