我试图将 Line 绑定到两个 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' 绑定表达式:路径=ActualCenter.X;DataItem='ScatterViewItem' (名称=''); 目标元素是'线'(名称='');目标属性是“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();
}
}