【问题标题】:WPF / MVVM / Model does not update viewmodel [duplicate]WPF / MVVM /模型不更新视图模型[重复]
【发布时间】:2020-01-17 19:45:09
【问题描述】:

上下文:

  • 我的 MVVM 应用程序中的绑定属性出现问题。现在我做了一个小项目来测试这个案例。它只包括窗口、视图模型、模型、BindableObject(带有 INotifyProperyChanged 的​​抽象类)和命令类。所有类都在同一个命名空间中,datacontext 在视图中设置,模型和 vm 具有 INotifyProperyChanged,视图中的文本绑定到绑定到模型属性的 vm 属性。构造函数设置模型属性并影响视图模型和视图中的属性。

问题:

  • 当我更改模型中的属性时,它不会更改视图模型和视图中的属性。

这里是 BaseModel 和 Command 类:

    abstract class BindableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertieChanged ([CallerMemberName]string name = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }


    class Command : ICommand
    {
        private Action<object> execute;
        private Func<object, bool> canExecute;

        public Command(Action<object> execute, Func<object, bool> canExecute = null)
        {
            this.execute    = execute;
            this.canExecute = canExecute;
        }

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

        public bool CanExecute(object parameter)
        {
            return this.canExecute == null || this.canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            this.execute(parameter);
        }
    }

型号:

    public class Model : BindableObject
    {
        private int modelValue;
        public int ModelValue { get => modelValue; set { modelValue = value; OnPropertieChanged(); } }

        public Model ()
        {
            ModelValue = 111;
        }

        public void ChangeValue ()
        {
            ModelValue = 777;
        }

    }

视图模型:

    class MainVM : BindableObject
    {
        private int myValue;
        public int MyValue { get => myValue; set { myValue = value; OnPropertieChanged(); } }

        public ICommand Command1 { get; set; }

        public MainVM()
        {
            var model = new Model();
            MyValue  = model.ModelValue;
            Command1 = new Command( (obj) => model.ChangeValue() );
        }

    }

查看:

    <Window>
    ...
        <Window.DataContext>
            <local:MainVM/>
        </Window.DataContext>

        <StackPanel>
            <TextBlock Text="{Binding MyValue}"/>
            <Button Command="{Binding Command1}"/>
        </StackPanel>

    </Window>

【问题讨论】:

    标签: c# wpf mvvm model viewmodel


    【解决方案1】:

    问题是您在视图模型中的命令正在调用模型实例上的方法,但您的 xaml 绑定到视图模型中的 int (MyValue) 类型的单独属性。这就是为什么 MyValue 在 vm 构造函数中设置一次,但从未使用命令更新。

    让你的 Model 类的实例成为视图模型中的一个属性:

            private Model _myModel;
    
            public Model MyModel
            {
                get
                {
                    return _myModel;
                }
                set
                {
                    _myModel = value;
                    OnPropertieChanged();
                }
            }
    
            public ICommand Command1 { get; set; }
    
            public MainVM()
            {
                MyModel = new Model();           
                Command1 = new Command((obj) => MyModel.ChangeValue());
            }
    
    

    并绑定到它在 xaml 中的 ModelValue 属性。

        <StackPanel>
            <TextBlock Text="{Binding MyModel.ModelValue}"/>
            <Button Height="50" Width="100" Command="{Binding Command1}"/>
        </StackPanel>
    

    不过,一般来说,模型类通常不执行逻辑。相反,逻辑要么在服务类中的模型实例上执行,要么在视图模型中的客户端执行。但这是一个单独的讨论。

    【讨论】:

    • 谢谢。尝试将模型制作为 vm var 但不是属性时,我是如此接近。
    猜你喜欢
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    相关资源
    最近更新 更多