之前我已成功实现了数据绑定,但现在它已经停止工作了...在放入一些断点后,我注意到PropertyChange事件仍然为null。我查了几个"解决方案"涉及使用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 :(得分: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();
}
}
如果要按属性为CurrentVelocity分配属性分配InitialVelocity。如下所示
newParticle.CurrentVelocity.AngleOfTravel = newParticle.InitialVelocity.AngleOfTravel;
newParticle.CurrentVelocity.HorizontalVelocity = newParticle.InitialVelocity.HorizontalVelocity;
newParticle.CurrentVelocity.Magnitude = newParticle.InitialVelocity.Magnitude;
newParticle.CurrentVelocity.VerticalVelocity = newParticle.InitialVelocity.VerticalVelocity;