WPF:尝试将一行绑定到两个 UIElement 时出现错误消息

发布于 2024-10-17 17:11:11 字数 1693 浏览 7 评论 0原文

我试图将一条线绑定到两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬 2024-10-24 17:11:11

我没有 Surface,但显然 ActualCenter.XActualCenter.Y 在分配实际值之前以 double.NaN 开始。由于这不会持续很长时间,因此您可以使用任何其他双精度值。因此,为了避免出现警告,您可以使用将 double.NaN 转换为 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();
    }
}

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文