属性不会获得NotifyPropertyChanged

时间:2014-09-08 16:02:03

标签: c# binding inotifypropertychanged

我有3个文件的小应用程序。第一个文件是Authentication,它继承自第二个文件ObservableObject。此文件继承INotifyPropertyChanged

class Authentication : ObservableObject
{
    public void Start()
    {
        Auth = Visibility.Visible;
        Tab = Visibility.Collapsed;
    }

    public void SetView()
    {
        Auth = Visibility.Collapsed;
        Tab = Visibility.Visible;
    }

    public Visibility Auth { get; set; }
    public Visibility Tab { get; set; }
    public Visibility Admin { get; set; }
    public Visibility Planner { get; set; }
    public Visibility WorkPrep { get; set; }
    public Visibility Leader { get; set; }
    public Visibility PreSet { get; set; }
    public Visibility Measure { get; set; }
    public Visibility Worker { get; set; }
}

我的第三个文件是我的View的ViewModel。

class MainWindowViewModel : ObservableObject
{
    private Authentication auth = new Authentication();

    public MainWindowViewModel()
    {
        LogIn = new RelayCommand(() => auth.SetView(), () => (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)) ? false : true);
        auth.Start();
    }

    public ICommand LogIn { get; set; }
    public Visibility Auth
    {
        get
        {
            return auth.Auth;
        }
        set
        {
            auth.Auth = value;
            NotifyPropertyChanged();
        }
    }
    public Visibility Tab
    {
        get
        {
            return auth.Tab;
        }
        set
        {
            auth.Tab = value;
            NotifyPropertyChanged();
        }
    }
}

现在,当我启动应用程序时auth.Start();执行正确并设置了正确的Visibility。当我按Button绑定到Command LogIn时,auth.SetView();已执行但Visibilities未更新。

我的结论是,当我加载应用程序时,Visibilities被设置正确但是一旦加载它就不会从Authentication类更新到MainWindowViewModel类。< / p>


编辑:这是ObservableObject类,对这个问题可能很重要。

public class ObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以通过Authentication类更新Auth的可访问性,这不会导致NotifyProperty事件被触发。我在Authentication类中添加了对NotifyPropertyChanged方法的调用:

class Authentication : ObservableObject
{
    public void Start()
    {
        Auth = Visibility.Visible;
        Tab = Visibility.Collapsed;
    }

    public void SetView()
    {
        Auth = Visibility.Collapsed;
        Tab = Visibility.Visible;
    }

    private Visibility auth;
    public Visibility Auth
    {
        get
        {
            return auth;
        }
        set
        {
            auth= value;
            NotifyPropertyChanged();
        }
    }

    private Visibility tab;
    public Visibility Tab
    {
        get
        {
            return tab;
        }
        set
        {
            tab = value;
            NotifyPropertyChanged();
        }
    }
    // etc
}

现在,我们只回显MainWindowViewModel中的每个NotifyProperty事件(在这种情况下这应该就足够了)。

class MainWindowViewModel : ObservableObject
{
    private Authentication auth = new Authentication();

    public MainWindowViewModel()
    {
        LogIn = new RelayCommand(() => auth.SetView(), () => (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)) ? false : true);

        // Echo the PropertyChanged events from our auth class
        auth.PropertyChanged += (sender, e) =>
        {
            if (PropertyChanged != null)
                PropertyChanged(sender, e);
        }

        auth.Start();
    }

    public ICommand LogIn { get; set; }
    public Visibility Auth
    {
        get
        {
            return auth.Auth;
        }
        set
        {
            auth.Auth = value;
            NotifyPropertyChanged();
        }
    }
    public Visibility Tab
    {
        get
        {
            return auth.Tab;
        }
        set
        {
            auth.Tab = value;
            NotifyPropertyChanged();
        }
    }
}