无法通过绑定在文本框中输入数据

问题描述 投票:0回答:1

我正在使用 MVVM 模式在 WPF 上创建图形编辑器。现在,我陷入了用户调用带有菜单的对话框窗口的时刻,并且当用户将数据输入到绑定到名称属性的文本框时,什么也没有发生。文本框中的值未更改。经过一些调试我发现 datacontext 的属性没有改变。

模型和视图类的父类

public class ModelViewModelBaseClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

型号

public class GraphModel : ModelViewModelBaseClass
{
    private string name { get; set; } = " - ";
    private Graph? graphObject { get; set; } = null;
    private bool isInitialized { get; set; }
    public string Name
    {
        get { return name; }
        set
        {
            if (isInitialized == true)
            { name = value; }
            else
            { name = " - "; }
            OnPropertyChanged("Name");
        }
    }
    public Graph GraphObject
    {
        get{return graphObject;}
        set
        {
            graphObject = value;
            OnPropertyChanged("GraphObject");
        }
    }
    public bool IsInitialized
    {
        get
        { return isInitialized; }
        set
        {
            isInitialized = value;
            OnPropertyChanged("IsInitialized");
        }
    }
}

视图模型

public class ApplicationViewModel : ModelViewModelBaseClass
{
    private GraphModel currentGraph;
    public GraphModel CurrentGraph
    {
        get { return currentGraph; }
        set
        {
            currentGraph = value;
            OnPropertyChanged("CurrentGraph");
        }
    }
    public ApplicationViewModel()
    {
        currentGraph = new GraphModel()
        {
            IsInitialized = false,
            Name = " - ",
            GraphObject = new Graph(" - ")
        };
    }

    private RelayCommand addGraphCommand;
    public RelayCommand AddGraphCommand
    {
        get
        {
            return addGraphCommand ??
                (addGraphCommand = new RelayCommand(o =>
                {
                    CreateGraphWindow createGraphWindow = new CreateGraphWindow(new GraphModel());
                    if (createGraphWindow.ShowDialog() == true)
                    {
                        GraphModel newGraphModel = createGraphWindow.CreatedGraph;
                        currentGraph = newGraphModel;                            
                    }
                }
                    ));
        }
    }
}

主窗口代码隐藏

public partial class MainWindow : Window
    {
        GraphViewer graphViewer = new GraphViewer();
        GraphModel graphModel = new GraphModel();
        Graph graph = new Graph();
        public MainWindow()
        {
            InitializeComponent();
            ApplicationViewModel applicationViewModel = new ApplicationViewModel();
            DataContext = applicationViewModel;
        }
    }

对话框窗口代码隐藏

    public partial class CreateGraphWindow : Window
    {
        public GraphModel CreatedGraph { get; private set; }
        public CreateGraphWindow(GraphModel createdGraph)
        {
            InitializeComponent();
            CreatedGraph = createdGraph;
            DataContext = CreatedGraph;
        }
        private void AcceptButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }
    } 

最小可重现示例的链接:https://github.com/TihonchukArtyom89/MVVM_WpfApp
我做错了什么以及如何纠正?


评论评论:

克劳斯·古特: 当前图=新图模型; - 你能看到这不会触发 PropertyChanges 事件吗?

c# wpf xaml mvvm
1个回答
0
投票

审阅者似乎指出的问题是您正在设置私有变量和公共变量,因此这绕过了通过

OnPropertyChanged("CurrentGraph");

引发的事件处理程序
    private GraphModel currentGraph; // don't set this
    public GraphModel CurrentGraph   // set this instead
    {
        get { return currentGraph; }
        set
        {
            currentGraph = value;
            OnPropertyChanged("CurrentGraph");
        }
    }

因此这段代码应该如下:


CreateGraphWindow createGraphWindow = new CreateGraphWindow(new GraphModel());
if (createGraphWindow.ShowDialog() == true)
{
    GraphModel newGraphModel = createGraphWindow.CreatedGraph;
                        
    // Capitol C CurrentGraph to set the one that triggers the event
    CurrentGraph = newGraphModel;                          
}

© www.soinside.com 2019 - 2024. All rights reserved.