数据绑定成功但 propertychange 仍然为空

Data Binding Successful but propertychange remains null

我之前成功实现了数据绑定,但现在它停止工作了...在设置一些断点后,我注意到 PropertyChange 事件仍然为空。我查找了几个涉及使用 DataContext 的 "solutions"(不确定将其放在哪里)但仍然没有用...

感谢您的帮助!

它适用于第一次初始绑定,但在它不起作用之后(当 属性 更改时)

我的代码: 我的绑定:

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

我分析了你的代码。在按钮单击事件 - 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 Assign 属性 by 属性。像下面这样

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