Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 361487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:53:37+00:00 2026-05-12T12:53:37+00:00

Still picking my way through learning XAML/WPF. If someone can show me how to

  • 0

Still picking my way through learning XAML/WPF. If someone can show me how to accomplish the following – it will go a long way to helping me develop my next project (and several similar projects down the road).

Say I have a collection of objects that defines objects to be drawn on a canvas. The objects contain all the information necessary to render the objects, including the shape, color, and location. Can a XAML control be created that binds to this collection and handles the rendering, or is this better done by drawing on the canvas in the code-behind?

One other point – the objects must eventually be click-selectable, selectable via rectangle-lasso, and draggable. This doesn’t have to be solved in the example code someone supplies, but I thought it might be relevant to know this as it might affect the various implementations.

Example class below. thanks in advance.

Class DrawingElement

  readonly property Shape as string    ("circle", "square", "triangle")
  readonly property Position as point  (coordinates)
  readonly property Color as string    ("red", "blue", "yellow")

end class

Sub Main

   dim lst as new List(of DrawingElement)

   lst.add(new DrawingElement("Circle", 10,20, "Blue"))
   lst.add(new DrawingElement("Square", 80,35, "Red"))
   lst.add(new DrawingElement("Triangle", 210,120, "Yellow"))

   <draw lst!>

End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T12:53:37+00:00Added an answer on May 12, 2026 at 12:53 pm

    Can be done, but not by using magic strings (e.g., “circle”) like in your example.

    First, you should be designing your models based on existing framework elements rather than designing the model with the idea of whipping up some new UI elements or struggling to create code that interprets between them.

    WPF already has an ellipse (circle), rectangle (square) and a whole host of other geometric primitives for you to use. You’ll want to create models that contain public bindable properties that you can bind to instances of these elements to control their shape and location.

    Without going into much detail (or testing), I’d do something like this

    public class GeometricElement : INotifyPropertyChanged
    {
      // these are simplified and don't show INPC code
      public double Left {get;set;} 
      public double Top {get;set;}
      public Brush Fill {get;set;}
      // ...
    }
    
    // ...
    
    public class Square : GeometricElement
    {
      public double Width {get;set;}
      public double Height {get;set;}
    }
    
    // ...
    
    // bound to the window
    public class CadAppDataContext
    {
      public ObservableCollection<GeometricElement> Elements {get; private set;}
    }
    

    And in my xaml, it would look something like

    <ItemsControl ItemsSource="{Binding Source={StaticResource cadAppDataContext}}" >
      <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
          <Canvas />
        </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>
      <ItemsControl.Resources>
        <DataTemplate TargetType="{x:Type me:Square}">
          <Rectangle 
             Canvas.Left="{Binding Left}"
             Canvas.Top="{Binding Top}"
             Width="{Binding Width}"
             Height="{Binding Height}"
             Fill="{Binding Fill}" />
        </DataTemplate>
        <!-- more DataTemplates for circle, triangle, etc etc -->
      </ItemsControl.Resources>
    </ItemsControl>
    

    The ItemsControl will create a Canvas element and for every GeometricElement in my Elements collection it will add a new child UI element based on the type of object in Elements.

    Items controls are bound to collections and can add or remove elements based on what’s happening in the collection within your code. It determines the UI element to add by looking for a DataTemplate that is designed for a particular Type. This is a common, and important, pattern and is why using magic strings will hurt you in the long run; the magic string route won’t let you leverage the power of the framework already built into WPF.

    Now, I’m not saying this will work out of the box. You’ll probably run into properties that won’t bind without some heavy lifting. You might even have to extend the geometry primitives to get them to behave how you want. But this is the pattern used in WPF applications. Understanding the pattern and using it will help you avoid hours of stress and failure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Still learning magento coding. I wonder is there a way I can print out
I'm still wading through Rails, picking up as much as I can. I have
I'm still picking up ObjC and I'm just trying to make sure I understand
Still working through JQuery to get data and repopulate portions of a page. Right
Still learning Objective-C / iPhone SDK here. I think I know why this wasn't
Still having issues with this problem. Please help if you can. So I am
I recently started picking up vi , going through some tutorials and trying to
Hoping someone can help me out with some pretty erratic behaviour from omnicomplete in
I'm slowly making my way through the rails source, to get a better grip
I've been picking my way though django-paypal documentation and have got a signal connecting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.