开发者

WPF: ErrorMessage when trying to Bind a Line to two UIElements

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();
        }
    }


I don't have Surface but apparently ActualCenter.X and ActualCenter.Y start out as double.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 translates double.NaN to O:

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();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜