【问题标题】:MVVM: how model can use inotifypropertychanged to notify viewmodel of changesMVVM:模型如何使用 inotifypropertychanged 通知 viewmodel 的变化
【发布时间】:2014-03-10 21:12:41
【问题描述】:

如果有任何属性发生更改,我需要 Model 通知 ViewModel,因为我需要将更改的模型实例收集到集合中以进行进一步处理,还需要启用和禁用视图模型中的命令按钮。 所以我使用了 ModelBase 抽象类并添加了 HasChanges 属性,我可以在视图模型中对其进行测试并捕获更改的模型。但它不起作用,我不知道我缺少什么。

public abstract class ModelBase : INotifyPropertyChanged
{
    protected ModelBase()
    {
    }

    private bool _hasChanges;
    public bool HasChanges
    {
        get
        { 
            return _hasChanges;
        }

        set
        {
            if (_hasChanges != value)
            {
                _hasChanges = value;
                RaisePropertyChanged("HasChanges");
            }
        }
    }

    protected void RaisePropertyChanged(string propertyName)
    {
        HasChanges = true;
        this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        var handler = this.PropertyChanged;

        if (handler != null)
        {
            handler(this, e);
        }
    }
}

Model 被包裹在 ViewModel 中,并绑定到 DataGrid 的 View:

private Model_selectedModel;

public Mode SelectedModel
{
    get 
    {
        return _selectedModel; 
    }

    set
    {
        if (_selectedModel != value)
        {
            _selectedModel = value;
            NotifyPropertyChanged("SelectedModel");
        }
    }
}

感谢您的宝贵帮助。

【问题讨论】:

    标签: c# wpf mvvm inotifypropertychanged


    【解决方案1】:

    我测试了你的课程,没关系。我认为这里的重点是一个错字:

    private Model_selectedModel;
    
    public Mode SelectedModel
    {
        get 
        {
            return _selectedModel; 
        }
    
        set
        {
            if (_selectedModel != value)
            {
                _selectedModel = value;
                NotifyPropertyChanged("SelectedModel");
            }
        }
    }
    

    应该有 RaisePropertyChanged 而不是 NotifyPropertyChanged

    下面是我的测试:

    XAML

    <Window x:Class="TestUpdatePropertyChanged.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:this="clr-namespace:TestUpdatePropertyChanged"
            Title="MainWindow" Height="350" Width="525">
    
        <Window.DataContext>
            <this:TestViewModel />
        </Window.DataContext>
    
        <Grid>
            <TextBox Width="100" Height="25" Text="{Binding Path=TestString, UpdateSourceTrigger=PropertyChanged}" />
    
            <Button Width="100" Height="30" VerticalAlignment="Top" Content="Click" Click="Button_Click" />
        </Grid>
    </Window>
    

    Code-behind

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var testData = this.DataContext as TestViewModel;
    
            testData.TestString = "Yay, it's change!";
    
            if (testData.HasChanges == true) 
            {
                MessageBox.Show("It's Change!");
            }
        }
    }
    
    public class TestViewModel : ModelBase 
    {
       private string _testString = "test";
    
        public string TestString
        {
            get { return _testString; }
    
            set
            {
                if (_testString != value)
                {
                    _testString = value;
                    RaisePropertyChanged("TestString");
                }
            }
        }
    } 
    
    public abstract class ModelBase : INotifyPropertyChanged
    {
        protected ModelBase()
        {
        }
    
        private bool _hasChanges;
        public bool HasChanges
        {
            get
            { 
                return _hasChanges;
            }
    
            set
            {
                if (_hasChanges != value)
                {
                    _hasChanges = value;
                    RaisePropertyChanged("HasChanges");
                }
            }
        }
    
        protected void RaisePropertyChanged(string propertyName)
        {
            HasChanges = true;
            this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = this.PropertyChanged;
    
            if (handler != null)
            {
                handler(this, e);
            }
        }
    }
    

    【讨论】:

    • @Anatoily :抱歉没有完全清楚,我的模型继承了 ModelBase。我的 ViewModel 实现了 INotifyPropertyChanged
    • @Hussein:您需要为 Model 或 ViewModel 确定 INotifyPropertyChanged - 不能同时用于两者。或者两者都继承自 Modelbase(你需要他的名字不同,例如:NotificationObject)。
    猜你喜欢
    • 2011-11-17
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 2012-08-21
    • 1970-01-01
    相关资源
    最近更新 更多