1

我试图将 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();
        }
    }
4

1 回答 1

2

我没有 Surface,但显然ActualCenter.XActualCenter.Y像在为double.NaN它们分配实际值之前一样开始。由于这不会持续很长时间,因此您可以使用任何其他双值来代替。因此,为避免出现警告,您可以使用转换double.NaNO

public class NormalizationConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var doubleValue = (double)value;
        return doubleValue == double.NaN ? 0 : doubleValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
于 2011-02-16T00:44:55.483 回答