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 8929651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:43:12+00:00 2026-06-15T08:43:12+00:00

In my program I am dynamically creating images to be added into a canvas

  • 0

In my program I am dynamically creating images to be added into a canvas in a WPF window.
My question is: How can I bind the canvas.left and canvas.right point to a class property.

If the image existed before run-time I would make and bind it like this in XAML/WPF:

<Image Height="26" HorizontalAlignment="Left" Canvas.Left="{Binding left}" Canvas.Top="{Binding top}" Name="Image1" Stretch="Fill" VerticalAlignment="Top" Width="28" Source="/imageTest;component/Images/blue-pin-md.png" />

What I have in VB.net:

'Create array of images
Dim myImages(5) as myImage

For i = 0 to myImages.count - 1
   myImages(i) = New myImage
   'set datacontext if I can bind
   myImages(i).image.DataContext = myImages(i)
   canvas1.Children.Add(myImages(i).image)
Next

myImage class:

Imports System.ComponentModel

Public Class myImage
    Implements INotifyPropertyChanged

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Private Sub NotifyPropertyChanged(ByVal info As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub

    Private Property _image as New Image
    Private Property _left As Double
    Private Property _top As Double
    Private Shared Property r As New Random()

    Public Sub New()
        _image.Width = 28
        _image.Height = 26
        _image.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/imageTest;component/Images/blue-pin-md.png", UriKind.Relative))
        'the below works without binding if I just want to set and leave them in one place but I would like to bind them so that I can move them relative to other data
        '_left = r.Next(0, System.Windows.SystemParameters.PrimaryScreenWidth)
        '_top = r.Next(0, System.Windows.SystemParameters.PrimaryScreenHeight)
    End Sub

    Public ReadOnly Property image As Image
        Get
            Return _image
        End Get
    End Property

    Public ReadOnly Property left As Double
        Get
            Return _left
        End Get
    End Property

    Public ReadOnly Property top As Double
        Get
            Return _top
        End Get
    End Property

    'a possible move method that would take advantage of the binding
    Public Sub move()
        _top += 1
        _left += 1
        NotifyPropertyChanged("left")
        NotifyPropertyChanged("top")
    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-06-15T08:43:13+00:00Added an answer on June 15, 2026 at 8:43 am

    Thanks to Clemens C# code I was able to get it working. Below is the code that I used. The .SetBindings(,) was the key for me.

    For i = 0 To myImages.Count - 1
        myImages(i) = New myImage
        myImages(i).image.DataContext = myImages(i)
        myImages(i).image.SetBinding(Canvas.LeftProperty, "left")
        myImages(i).image.SetBinding(Canvas.TopProperty, "top")
        canvas1.Children.Add(myImages(i).image)
    Next
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently creating a program that dynamically adds Image to a dockPanel every
User Defined Function (UDFs) are functions that one can program and can be dynamically
When creating dlls (Add-ins) for a third party program that loads Native DLLs dynamically,
I'm looking into creating a Plugin structure for a program and making it so
I have a program that I am creating that creates task inputs dynamically. My
I have a program which dynamically generates a GUI. I don't know how many
In my program I generate classes dynamically but when I try: String[] args =
I have a Flash program that loads AS2 and AS3 SWF files dynamically with
How could I write a simple program using OpenType tables in order to dynamically
I am dynamically creating a table that contains a textbox in each cell. The

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.