MVVM:模型如何使用inotifypropertychanged来通知视图模型的更改



如果任何属性被更改,我需要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);
        }
    }
}

模型被包装在ViewModel中,并绑定到视图,这是一个DataGrid:

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

感谢您的宝贵帮助。

我测试了你的类,它是好的。我想关键是这里有个打字错误:

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);
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新