C# 系统转换器。图纸。点';至';System.Windows.Point

C# 系统转换器。图纸。点';至';System.Windows.Point,c#,wpf,xaml,valueconverter,C#,Wpf,Xaml,Valueconverter,我试图在WPF中绘制一些实体。我的集合包含System.Drawing.Rectangle对象,当我尝试在WPF XAML中访问这些对象的位置时,我遇到以下错误 无法创建默认转换器以在“System.Drawing.Point”和“System.Windows.Point”类型之间执行“单向”转换。考虑绑定转换器属性 我知道我必须使用一些值转换器。您能指导我如何将System.Drawing.Point转换为System.Windows.Point吗 更新: 下面的代码给出了一些异常 publi

我试图在WPF中绘制一些实体。我的集合包含System.Drawing.Rectangle对象,当我尝试在WPF XAML中访问这些对象的位置时,我遇到以下错误

无法创建默认转换器以在“System.Drawing.Point”和“System.Windows.Point”类型之间执行“单向”转换。考虑绑定

转换器属性 我知道我必须使用一些值转换器。您能指导我如何将System.Drawing.Point转换为System.Windows.Point吗

更新:

下面的代码给出了一些异常

public class PointConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        System.Windows.Point pt = (Point)(value);
        return pt;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
XAML:


我想你会得到
InvalidCastException
,你不能将一种类型强制转换为另一种类型,除非它们之间存在隐式或显式转换。记住,施法和转化是不同的。以下代码将
System.Drawing.Point
转换为
System.Windows.Point
,反之亦然

public class PointConverter : System.Windows.Data.IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        System.Drawing.Point dp = (System.Drawing.Point)value;
        return new System.Windows.Point(dp.X, dp.Y);
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        System.Windows.Point wp = (System.Windows.Point) value;
        return new System.Drawing.Point((int) wp.X, (int) wp.Y);
    }
}


如果
System.Drawing.Point
来自Windows窗体鼠标事件,例如单击事件,则
System.Drawing.Point
不能以这种方式直接转换为
System.Windows.Point
,因为每个点的坐标系可能不同。有关更多信息,请参阅。

我想您可能会遇到
InvalidCastException
,除非它们之间存在隐式或显式转换,否则不能将一种类型强制转换为另一种类型。记住,施法和转化是不同的。以下代码将
System.Drawing.Point
转换为
System.Windows.Point
,反之亦然

public class PointConverter : System.Windows.Data.IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        System.Drawing.Point dp = (System.Drawing.Point)value;
        return new System.Windows.Point(dp.X, dp.Y);
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        System.Windows.Point wp = (System.Windows.Point) value;
        return new System.Drawing.Point((int) wp.X, (int) wp.Y);
    }
}


如果
System.Drawing.Point
来自Windows窗体鼠标事件,例如单击事件,则
System.Drawing.Point
不能以这种方式直接转换为
System.Windows.Point
,因为每个点的坐标系可能不同。有关更多信息,请参阅。

为什么要投反对票?让我知道,我可以在将来纠正它。我是落选的选民,现在被撤职了。原因是你没有表现出你的尝试,只是要求把代码给我。在您的编辑问题看起来很好之后。@SriramSakthivel,非常感谢。现在我明白了,我的回答对你有帮助吗?别忘了标记为答案,如果这有助于反对票的人?让我知道,我可以在将来纠正它。我是落选的选民,现在被撤职了。原因是你没有表现出你的尝试,只是要求把代码给我。在您的编辑问题看起来很好之后。@SriramSakthivel,非常感谢。现在我明白了,我的回答对你有帮助吗?如果有帮助的话,别忘了标上答案