从另一个用户控件调用事件时,不会刷新Binded属性

时间:2017-01-20 13:25:39

标签: wpf mvvm data-binding

简化情况。我有MainWindow有两个用户控件,所有控件都有相应的Viewmodels。除了一个功能外,一切正常,属性绑定等等。

我想在第一个事件发生后刷新第二个用户控件的数据。不幸的是,在这种情况下,PropertyChanged事件(从ViewModelBase中定义的INotifyPropertyChanged派生)为空。

但是,如果我从第二个用户控件引发事件,则视图上的属性会按预期更新!

public class MainWindowViewModel : ViewModelBase
{
      public FirstUserControl FirstUserControl {get; set;}
      public SecondUserControl SecondUserControl {get; set;}

      public MainWindowViewModel ()
      {
             FirstUserControl =new FirstUserControl();
             FirstUserControl.RaiseClicked+=OnRaiseClicked;

             SecondUserControl = new SecondUserControl();
             SecondUserControl .RaiseClicked+=OnRaiseClicked;
      }

      private void OnRaiseClicked(object sender, EventArgs e)
      {
            SecondUserControl.RefreshView();
      }
}



public class FirstUserControl : ViewModelBase
{
      public ICommand Raise { get; private set; }

      public EventHandler RaiseClicked {get;set;}

      public FirstUserControl ()
      {
             Raise = new RelayCommand( p=> RaiseClicked(this, null));
      }
}

public class SecondUserControl: ViewModelBase
{
      public ICommand Raise { get; private set; }

      public EventHandler RaiseClicked {get;set;}

      public string Title
      {  
           get
           {
                return MyLogic.GetCurrentTitle(); // debuggers enter here only while event on second user control raised
           }
      }

      public void  RefreshView()
      { 
            OnPropertyChanged("Title"); // debugger enter here in cases
      }
}

我想有一些线程正在进行,但我并不熟悉WPF来自己解决它。有人可以帮助如何快速轻松地从第二个UC刷新数据中做出事件吗?

0 个答案:

没有答案