Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVVM:模型如何使用inotifypropertychanged通知viewmodel更改_C#_Wpf_Mvvm_Inotifypropertychanged - Fatal编程技术网

C# MVVM:模型如何使用inotifypropertychanged通知viewmodel更改

C# MVVM:模型如何使用inotifypropertychanged通知viewmodel更改,c#,wpf,mvvm,inotifypropertychanged,C#,Wpf,Mvvm,Inotifypropertychanged,如果任何属性发生更改,我需要Model通知ViewModel,因为我需要在集合中收集更改的模型实例以供进一步处理,还需要在ViewModel中启用和禁用命令按钮。 所以我使用了ModelBase抽象类并添加了HasChanges属性,我可以在viewmodel中对其进行测试并捕获更改后的模型。但是它不起作用,我不知道我缺少了什么 public abstract class ModelBase : INotifyPropertyChanged { protected ModelBase()

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

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>

@阿纳托利:很抱歉没有完全弄清楚,是我的模型继承了ModelBase。我的ViewModel实现INotifyPropertyChanged@Hussein:您需要确定模型的
INotifyPropertyChanged
,或
ViewModel
——而不是两者的。或者两者都继承自Modelbase(您只需要他的名字不同,例如:
NotificationObject
)。
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);
        }
    }
}