C# 类型转换器和数据绑定

C# 类型转换器和数据绑定,c#,.net,formatting,typeconverter,C#,.net,Formatting,Typeconverter,在解决弧度和角度之间的转换(类型转换)问题时,我遇到了麻烦 在Win Forms中,我有一个文本框,它绑定到类“Triangle”的属性AngleX 我的属性以弧度存储值,但我希望以角度格式向用户显示,如果用户在UI中更改它,我需要以弧度将其返回给类 在这一点上,我有以下代码: public RadAngleConverter : TypeConverter { public override bool CanConvertTo(ITypeDescriptorContext context

在解决弧度和角度之间的转换(类型转换)问题时,我遇到了麻烦

在Win Forms中,我有一个文本框,它绑定到类“Triangle”的属性AngleX

我的属性以弧度存储值,但我希望以角度格式向用户显示,如果用户在UI中更改它,我需要以弧度将其返回给类

在这一点上,我有以下代码:

public RadAngleConverter : TypeConverter
{
   public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return destinationType == typeof(Int64);
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        return  Math.PI * (Int64)value / 180.0;
    }

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType == typeof(Int64);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        return (Int64)value * (180.0 / Math.PI);             
    }
}
在我的“三角形”课程中,我有以下代码:

public class Triangle()
{
...
        [TypeConverter(typeof(RadGrausFormat))] 
        public double AngleX { get; set; }
...
}
...
 TextBox1.DataBindings.Add("Text",instanceClass,"AngleX",formattingEnable: true)
...
最后,在UI中,此代码:

public class Triangle()
{
...
        [TypeConverter(typeof(RadGrausFormat))] 
        public double AngleX { get; set; }
...
}
...
 TextBox1.DataBindings.Add("Text",instanceClass,"AngleX",formattingEnable: true)
...
但它不起作用


有人能帮我解决这个问题吗?

return destinationType==typeof(Int64)上放置断点的可能重复
并检查参数。我猜它正在尝试转换为字符串而不是Int64。程序不会转到这一点。我无法调试它。我要做的是用
AngleXDegrees
创建第二个属性并绑定到它
public double AngleXDegrees{get{return AngleX*DegreesPerRadian;}}}
然后你就可以开始新的一天了!在
返回destinationType==typeof(Int64)上放置断点的可能重复项
并检查参数。我猜它正在尝试转换为字符串而不是Int64。程序不会转到这一点。我无法调试它。我要做的是用
AngleXDegrees
创建第二个属性并绑定到它
public double AngleXDegrees{get{return AngleX*DegreesPerRadian;}}}
然后你就可以开始新的一天了!