WPF:尝试将一行绑定到两个 UIElement 时出现错误消息
我试图将一条线绑定到两个 ScatterViewItems:
private void BindLineToScatterViewItems(Shape line, ScatterViewItem origin, ScatterViewItem destination)
{
// Bind line.(X1,Y1) to origin.ActualCenter
BindingOperations.SetBinding(line, Line.X1Property, new Binding { Source = origin, Path = new PropertyPath("ActualCenter.X") });
BindingOperations.SetBinding(line, Line.Y1Property, new Binding { Source = origin, Path = new PropertyPath("ActualCenter.Y") });
// Bind line.(X2,Y2) to destination.ActualCenter
BindingOperations.SetBinding(line, Line.X2Property, new Binding { Source = destination, Path = new PropertyPath("ActualCenter.X") });
BindingOperations.SetBinding(line, Line.Y2Property, new Binding { Source = destination, Path = new PropertyPath("ActualCenter.Y") });
}
但我总是收到以下错误消息:
System.Windows.Data 错误:5:值 BindingExpression 生成的不是 对目标财产有效。; 值='NaN' BindingExpression:Path=ActualCenter.X; DataItem='ScatterViewItem'(名称=''); 目标元素是“Line”(名称=“”); 目标属性是“X1”(类型 “双”)
尽管如此,它正在工作,但是我怎样才能抑制这个警告呢?为什么会显示这个警告?
编辑:根据下面的答案,我现在使用以下转换器,但仍然收到错误:
public class NormalizationConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (double) value == double.NaN ? Binding.DoNothing : (double) value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Im trying to bind a Line to two ScatterViewItems:
private void BindLineToScatterViewItems(Shape line, ScatterViewItem origin, ScatterViewItem destination)
{
// Bind line.(X1,Y1) to origin.ActualCenter
BindingOperations.SetBinding(line, Line.X1Property, new Binding { Source = origin, Path = new PropertyPath("ActualCenter.X") });
BindingOperations.SetBinding(line, Line.Y1Property, new Binding { Source = origin, Path = new PropertyPath("ActualCenter.Y") });
// Bind line.(X2,Y2) to destination.ActualCenter
BindingOperations.SetBinding(line, Line.X2Property, new Binding { Source = destination, Path = new PropertyPath("ActualCenter.X") });
BindingOperations.SetBinding(line, Line.Y2Property, new Binding { Source = destination, Path = new PropertyPath("ActualCenter.Y") });
}
But I always get the following error message:
System.Windows.Data Error: 5 : Value
produced by BindingExpression is not
valid for target property.;
Value='NaN'
BindingExpression:Path=ActualCenter.X;
DataItem='ScatterViewItem' (Name='');
target element is 'Line' (Name='');
target property is 'X1' (type
'Double')
Nevertheless it is working, but how can I surpress this warning? And why is this warning displayed?
EDIT: According to the answer below, I use now following converter, but still get the errors:
public class NormalizationConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (double) value == double.NaN ? Binding.DoNothing : (double) value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有 Surface,但显然
ActualCenter.X
和ActualCenter.Y
在分配实际值之前以double.NaN
开始。由于这不会持续很长时间,因此您可以使用任何其他双精度值。因此,为了避免出现警告,您可以使用将 double.NaN 转换为 O 的转换器:I don't have Surface but apparently
ActualCenter.X
andActualCenter.Y
start out asdouble.NaN
before they are assigned their actual values. Since this doesn't last long, you could you any other double value instead. So to avoid the warning, you can use a converter that translatesdouble.NaN
toO
: