2015-02-08 56 views
0

我已經成功實施了一個數據綁定,但現在它已停止工作......放入一些斷點後,我注意到PropertyChange事件保持爲空。我查找了幾個涉及使用DataContext(不確定放在哪裏)的「解決方案」,但仍然沒有工作...數據綁定成功但財產更改仍然爲空

感謝您的幫助!

它適用於第一初始綁定,但它確實後不入鍋(當屬性發生變化)

我的代碼: 我的綁定:

 //databinding 
     Binding(newProjectile.CurrentVelocity, lbl_angleoftraveloutput, "AngleOfTravel"); 

    private void Binding(velocity Object, Label Output, string Field) 
    { 
     Binding newBinding = new Binding(); 
     newBinding.Source = Object; 
     newBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 
     newBinding.Path = new PropertyPath(Field); 
     newBinding.Mode = BindingMode.TwoWay; 
     Output.SetBinding(Label.ContentProperty, newBinding); 
    } 

我的對象(的一部分):

public class velocity : INotifyPropertyChanged, ICloneable 
{ 
    public double AngleOfTravel 
    { 
     get { return _AngleOfTravel; } 
     set 
     { 
      _AngleOfTravel = value; 
      OnPropertyChanged("AngleOfTravel"); 
     } 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
    protected void OnPropertyChanged(string PropertyName) 
    { 

     PropertyChangedEventHandler Handler = PropertyChanged; 
     if (Handler != null) 
     { 
      Handler(this, new PropertyChangedEventArgs(PropertyName)); 
     } 
    } 
} 

回答

0

我已經分析過你的代碼。在按鈕單擊事件 - LoadStaticsAndStart你有一個聲明newParticle.CurrentVelocity = newParticle.InitialVelocity;這是他造成這個問題的原因。 newParticle.CurrentVelocity綁定到UI標籤,所以它將具有INotifyPropertyChanged實現。但newParticle.InitialVelocity沒有綁定到UI,所以它不會有INotifyPropertyChanged,所以我已經爲你輸出標籤和UI更新發表了評論。用下面的功能替換功能。

private void LoadStaticsAndStart(object sender, RoutedEventArgs e) 
    { 
     GraphCanvas.Visibility = Visibility.Visible; 
     DrawAxis(); 
     List<velocity> velocityList = new List<velocity>(); 
     double[,] DisplacementArray = new double[59, 1]; 
     btn_Calculate.Click += OnLoaded; 
     particle newParticle; 
     velocity InitialVelocity = new velocity(); 
     ObjectsAndDataStuctures.Environment newEnvironment; 
     if (FormatCheck(txtb_AngleOLaunch) == false || FormatCheck(txtbox_InitialVelocity) == false || FormatCheck(txtbox_TimeOfFlight) == false) 
     { 
      MessageBox.Show(" Input is not valid"); 
     } 
     else 
     { 
      newEnvironment = new ObjectsAndDataStuctures.Environment(); 
      newEnvironment.gravity = -9.8; // gravity remove when needed or changed 
      InitialVelocity.Magnitude = Convert.ToDouble(txtbox_InitialVelocity.Text); // collecting variables. 
      InitialVelocity.AngleOfTravel = Convert.ToDouble(txtb_AngleOLaunch.Text); // collecting variables. 
      newParticle = new particle(InitialVelocity, Convert.ToDouble(txtbox_TimeOfFlight.Text)); 
      stk_pnl_inputvar.Visibility = Visibility.Collapsed; 
      CreateLabels(newParticle); 
      newParticle.CalculateHComponent(); 
      newParticle.CalculateVComponent(); 
      MPP(newParticle, newEnvironment); 
      OnLoaded(this, e); 
      if (Convert.ToDouble(txtbox_TimeOfFlight.Text) > 60) 
      { 
       TimeConstant = Convert.ToDouble(txtbox_TimeOfFlight.Text)/60; 
      } 
      else 
      { 
       TimeConstant = 1; 
      } 
      double HVelTemp = newParticle.InitialVelocity.HorizontalVelocity * PixelPerMeter; 
      double VVelTemp = newParticle.InitialVelocity.VerticalVelocity * PixelPerMeter * -1; 
      Velocity = new Vector(HVelTemp, VVelTemp); // y direction is downwards 
      acceleration = new Vector(0, -1 * newEnvironment.gravity * PixelPerMeter); // y direction is downwards 
      aTimer = new System.Windows.Threading.DispatcherTimer(); 
      aTimer.Interval = new TimeSpan(0, 0, 0, 0, 200); 
      //newParticle.CurrentVelocity = newParticle.InitialVelocity; 
      //this.DataContext = this; 
      TimerEvent = (s, t) => onTimedEvent(sender, t, newParticle, newEnvironment); 
      aTimer.Tick += TimerEvent; 
      ListOfVelocities.Add((velocity)newParticle.CurrentVelocity.Clone()); 
      InSimulation = true; 
      aTimer.Start(); 
     } 
    } 

如果你想爲你指定InitialVelocity和CurrentVelocity按屬性指定屬性。這樣的下面

newParticle.CurrentVelocity.AngleOfTravel = newParticle.InitialVelocity.AngleOfTravel; 
       newParticle.CurrentVelocity.HorizontalVelocity = newParticle.InitialVelocity.HorizontalVelocity; 
       newParticle.CurrentVelocity.Magnitude = newParticle.InitialVelocity.Magnitude; 
       newParticle.CurrentVelocity.VerticalVelocity = newParticle.InitialVelocity.VerticalVelocity; 
+0

對不起,我複製一個粘貼錯誤的事情:/我只是複製和我對象粘貼的部分 - 現在編輯 – ObnoxiousFrog 2015-02-08 14:54:32

+0

我編輯我的答案 – 2015-02-08 15:02:02

+0

我仍然有同樣的問題:/它綁定在開始但隨着值的變化,它的事件仍爲空 – ObnoxiousFrog 2015-02-08 15:13:57

相關問題