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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:30:19+00:00 2026-05-30T08:30:19+00:00

I am trying to create a UserControl that is draggable in a Canvas .

  • 0

I am trying to create a UserControl that is draggable in a Canvas. I am brand new to MVVM and relatively new to WPF (also pretty new to StackOverflow).

I have a view model, which implements INotifyPropertyChanged, for the new UserControl that has properties called “Top” and “Left”. I would like to bind the Top and Left properties of my view model to the attached Canvas.Left and Canvas.Top of the UserControl. For reasons I won’t get into, I can’t use any XAML.

This is how I’ve implemented my Left (and similarly Top) properties (not sure how to make code highlighting work yet):

public class FooViewModel : INotifyPropertyChanged
{
    // . . .
    double _left;
    public double Left 
    { 
        get { return _left; }
        set
        {
            if(_left != value)
            {
                _left = value;
                NotifyPropertyChanged("Left");
            }
        }
    }
    // . . .
}

This is how I’ve implemented my ‘UserControl’:

public class FooControl : UserControl
{
    // . . .
    private FooViewModel _vm;

    public FooControl(FooViewModel vm)
    {
        _vm = vm;
        this.DataContext = _vm;

        Binding b = new Binding("Left");
        b.Mode = BindingMode.TwoWay;
        this.SetBinding(Canvas.LeftProperty, b);

        // . . .
    }
    // . . .
}

To test, I have manually created some view model instances, set the Left and Top properties on them, created instances of my UserControl by passing these to the constructor, and added them to a ‘Canvas’. Unfortunately, all of my added controls show up in the top-left corner of the ‘Canvas’ and the debugger shows that while the Top and Left properties of the view model are set correctly, calling Canvas.GetLeft(...) and Canvas.GetTop(...) on the controls return NaN.

What am I doing wrong? Am I taking the wrong approach?

I’ve tried to base my code around this question & answer.

EDIT:
Actually, this does work! I had mistake in my code where the UserControl wasn’t getting a proper reference to an instance of the view model. Once I hooked that up correctly it worked fine. Thanks everyone!

  • 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-30T08:30:21+00:00Added an answer on May 30, 2026 at 8:30 am

    That code should work just fine.

    Are the controls actually immediate children of the canvas? If not it will not work.

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

Sidebar

Related Questions

I'm trying to create a reusable UserControl in WPF that has a Label and
I'm relatively new to OOD, C#, WPF, but trying to learn. So I have
I'm trying to create a UserControl that inherits from a generic class. It does
I am trying create a WCF service that leverages the WPF MediaPlayer on the
I'm trying to create a user control that will provide a draggable splitter between
I am trying to create a user control within a WPF application that will
I'm trying to create a custom UserControl that will mimic auto-complete as it works
I am trying to bind a userControl that I create in a class as
I am trying to create a composite DataContext for a UserControl. Basically I have
I'm trying to create a UserControl that contains a ListView and a number of

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.