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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:09:57+00:00 2026-05-15T04:09:57+00:00

I am in the process of taking a Silverlight 4 UserControl containing a canvas

  • 0

I am in the process of taking a Silverlight 4 UserControl containing a canvas which has a number of FrameworkElements on it and converting this to use databinding.

The XAML for my original canvas was:

<Canvas x:Name="panelDisplay" >
    <Rectangle Width="50" Height="50" MouseLeftButtonDown="Element_MouseLeftButtonDown" Stroke="Aqua" StrokeThickness="5" Fill="Aquamarine" Canvas.Left="450" Canvas.Top="50" x:Name="rect1" />
    <Image Source="../Images/3.jpg" Stretch="UniformToFill" Width="356" Height="224" MouseLeftButtonDown="Element_MouseLeftButtonDown" Canvas.Left="317" Canvas.Top="140" x:Name="image1" />
</Canvas>

This displays the rectangle and image and the MouseLeftButtonDown event fires which then deals with operations such as dragging and resizing.

In order to get this working with databinding I created an object called CanvasElement:

public class CanvasElement
{
    public CanvasElement(int id, object elementContent, double width, double height, int left, int top)
    {
        Id = id;
        ElementContent = elementContent;
        Width = width;
        Height = height;
        Left = left;
        Top = top;
    }

    public int Id { get; set; }
    public object ElementContent { get; set; }
    public double Width { get; set; }
    public double Height { get; set; }
    public int Left { get; set; }
    public int Top { get; set; }
}

The ElementContent is used to store the Rectangle or Image. I populate an ObservableCollection called CanvasElements and assign the DataContext of the control. I have changed my XAML to:

<Canvas x:Name="panelDisplay" >
    <ItemsControl x:Name="CanvasElements" ItemsSource="{Binding Path=CanvasElements}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Canvas>
                    <ContentControl Content="{Binding Path=ElementContent}" Height="{Binding Path=Height}" Width="{Binding Path=Width}" 
                                Canvas.Left="{Binding Path=Left}" Canvas.Top="{Binding Path=Top}"
                                MouseLeftButtonDown="CanvasElement_MouseLeftButtonDown" />
                </Canvas>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Canvas>

My elements appear but the CanvasElement_MouseLeftButtonDown does not fire. I have also used ContentControl within the ItemsControl.

My questions are:

Is this is sensible way to go?

Why is MouseLeftButtonDown not firing?

  • 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-15T04:09:58+00:00Added an answer on May 15, 2026 at 4:09 am

    I eventually decided that using the ItemsControl to bind to a collection of shapes was not the best way forward.

    The main reason for this is that by using a control in the DataTemplate to house shapes adds extra complexity, in my case I have a main canvas and each shape was housed in its own canvas placed on the main canvas.

    I believe this was causing the problem with the events and makes it more complex to position shapes.

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

Sidebar

Related Questions

I want to process every element of a for-loop. Taking this code, why is
I'm trying to dot product two vectors, with each process taking on a separate
I am in the process of implementing jQuery, and taking out Prototype libraries in
We want to apply OO to design a process of taking an exam and
I've been working on a silverlight application which generates various graphs. It requires a
I have a time taking process in DoWork of my BackgroundWorker . Whenever I
I'm writing a shell script to find out if any process is taking too
I've been taking heapshots of a certain process. All the leaked objects in all
I believe I have been taking the right approach to this so far, but
My company is in the process of taking over a members only website from

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.