C# WPF代码绑定不起作用
我在代码绑定方面有问题 我有X,Y双重属性和依赖属性的控件C# WPF代码绑定不起作用,c#,wpf,binding,C#,Wpf,Binding,我在代码绑定方面有问题 我有X,Y双重属性和依赖属性的控件 public double X { get { return (double)GetValue(XProperty); } set { SetValue(XProperty, value); } } public double Y { get { return (double)GetValue
public double X
{
get { return (double)GetValue(XProperty); }
set
{
SetValue(XProperty, value);
}
}
public double Y
{
get { return (double)GetValue(YProperty); }
set
{
SetValue(YProperty, value);
}
}
public static readonly DependencyProperty XProperty = DependencyProperty.Register("X", typeof(double), typeof(VisualPin));
public static readonly DependencyProperty YProperty = DependencyProperty.Register("Y", typeof(double), typeof(VisualPin));
在另一个控件中,我有一条线,我尝试使用绑定到线端点,如下所示:
var xBinding = new Binding("XProperty") {Source = _startPin};
var yBinding = new Binding("YProperty") {Source = _startPin};
Line.SetBinding(Line.X1Property, xBinding);
Line.SetBinding(Line.Y1Property, yBinding);
所有属性中的所有数据都正常,我检查过了,但绑定不起作用。我不知道为什么。。。(所有控件都位于一个画布上)
多谢各位 要调试这种情况,您应该使用snoop WPF之类的工具查看XAML。这将显示实际绑定是什么 可能是绑定在那里,但没有更新。要解决此问题,您的类应该实现INotifyPropertyChanged。无论何时要更新该值,例如在设置该值之后,都可以使用属性的名称调用以下函数:
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
对不起,是我的错。问题出现在寄存器依赖项属性的第一个参数中
public static readonly DependencyProperty XProperty = DependencyProperty.Register("X", typeof(double), typeof(VisualPin));
public static readonly DependencyProperty YProperty = DependencyProperty.Register("Y", typeof(double), typeof(VisualPin));
而不是:
public static readonly DependencyProperty XProperty = DependencyProperty.Register("XProperty", typeof(double), typeof(VisualPin));
public static readonly DependencyProperty YProperty = DependencyProperty.Register("YProperty", typeof(double), typeof(VisualPin));
对不起,是我的错。问题出在寄存器依赖项属性-X的第一个参数中,而不是XProperty!请把这个帖子作为回答并接受它,或者考虑删除这个问题,如果你没有预料到社区的任何事情。谢谢