Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

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

I know I have to use some valueconverter. Could you please guide me how to convert System.Drawing.Point' to'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:

<PathFigure StartPoint= "{Binding BoundingRect.Location, Converter={StaticResource PointConverter}}">
                I was the downvoter, now removed. Reason is you haven't shown your attempt and just asking like give me the code. After your edit question looks fine.
– Sriram Sakthivel
                Apr 3, 2014 at 4:25

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.Point to System.Windows.Point and viceversa.

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);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.