wpf System.Drawing.Point' 到 'System.Windows.Point' 的转换器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22827412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:27:13  来源:igfitidea点击:

Converter of System.Drawing.Point' to 'System.Windows.Point

c#wpfxamlvalueconverter

提问by RobinAtTech

I am trying to draw few entities in WPF. My collection contains System.Drawing.Rectangle objects, When I try to access the location of those objects in WPF XAML I am getting following error

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

Cannot create default converter to perform 'one-way' conversions between types 'System.Drawing.Point' and 'System.Windows.Point'. Consider using Converter property of Binding

无法创建默认转换器以在类型“System.Drawing.Point”和“System.Windows.Point”之间执行“单向”转换。考虑使用 Binding 的 Converter 属性

I know I have to use some valueconverter. Could you please guide me how to convert System.Drawing.Point' to'System.Windows.Point?

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

Update:

更新:

Following code gives some exception

以下代码给出了一些例外

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:

XAML:

<PathFigure StartPoint= "{Binding BoundingRect.Location, Converter={StaticResource PointConverter}}">

回答by Sriram Sakthivel

I guess you'd have got InvalidCastException, you can't just cast one type to another unless implicit or explicit conversion exist between them. Remember cast is different and convert is different. Following code converts System.Drawing.Pointto System.Windows.Pointand viceversa.

我想你已经得到了InvalidCastException,除非它们之间存在隐式或显式转换,否则不能将一种类型强制转换为另一种类型。记住 cast 是不同的,convert 是不同的。以下代码转换System.Drawing.PointSystem.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);
    }
}


If the System.Drawing.Pointcomes from a Windows Forms mouse event, such as a click event, a System.Drawing.Pointcan't be directly converted to System.Windows.Pointin this way, since the coordinate systems of each may differ. See https://stackoverflow.com/a/19790851/815724for more information.

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