我正在尝试在 WPF 中绘制一些实体。我的集合包含 System.Drawing.Rectangle 对象,当我尝试在 WPF XAML 中访问这些对象的位置时,出现以下错误
无法创建默认转换器来执行类型“System.Drawing.Point”和“System.Windows.Point”之间的“单向”转换。考虑使用 Binding 的 Converter 属性
我知道我必须使用一些值转换器。您能指导我如何将 System.Drawing.Point' 转换为 System.Windows.Point 吗?
Update:
以下代码给出了一些异常
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:
<PathFigure StartPoint= "{Binding BoundingRect.Location, Converter={StaticResource PointConverter}}">
我猜你会有InvalidCastException
,除非它们之间存在隐式或显式转换,否则不能将一种类型强制转换为另一种类型。请记住演员不同,转换也不同。以下代码转换System.Drawing.Point
to 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);
}
}
If the System.Drawing.Point
来自 Windows 窗体鼠标事件,例如单击事件、System.Drawing.Point
不能直接转换为System.Windows.Point
这样,由于每个坐标系可能不同。看https://stackoverflow.com/a/19790851/815724 https://stackoverflow.com/a/19790851/815724了解更多信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)