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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:15:30+00:00 2026-05-16T02:15:30+00:00

I want to be able to implement an ItemsControl with dragable items. The reason

  • 0

I want to be able to implement an ItemsControl with dragable items. The reason for the ItemsControl is so I can bind to my ViewModel in the background.

I’ve tried using a Thumb Control in a canvas and it works perfect, except as soon as I stick it in an ItemsControl it stops working. Here is what I tried:

        <ItemsControl ItemsSource="{Binding MyItems}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Thumb Canvas.Left="0" Canvas.Top="0" Width="50" Height="50" DragDelta="MyThumb_DragDelta"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

    </ItemsControl>

The code behind:

    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = new MainViewModel();
    }

    private void MyThumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
    {
        Canvas.SetLeft((UIElement)sender, Canvas.GetLeft((UIElement)sender) + e.HorizontalChange);
        Canvas.SetTop((UIElement)sender, Canvas.GetTop((UIElement)sender) + e.VerticalChange);
    }

And finally my ViewModel:

    public class MainViewModel : DependencyObject 
{
    public ObservableCollection<Note> MyItems { get; set;}


    public MainViewModel()
    {
        MyItems = new ObservableCollection<Note>();
        MyItems.Add(new Note(){Name="test"});
    }

}

public class Note : INotifyPropertyChanged 
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string name;

    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            if(PropertyChanged!=null) PropertyChanged(this,new PropertyChangedEventArgs("Name"));
        }
    }


}

When I do the following on the window it works fine:

  <Canvas>
        <Thumb Canvas.Left="0" Canvas.Top="0" Width="50" Height="50" DragDelta="MyThumb_DragDelta"/>            
    </Canvas>

But when I have it in an ItemsControl it no longer works. I assume the ItemsControl is Registering for mouse events and overriding the Thumb?

Anyone have a good solution to get getting this working?

  • 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-16T02:15:31+00:00Added an answer on May 16, 2026 at 2:15 am

    Ben I didn’t think that approach worked at first but after more experimenting I got it.

    The problem could be boiled down to: Canvas.Top and Canvas.Left don’t work while in an items control. But you are correct that the style is the way to get around the problem. Here is the solution I came up with:

    <ItemsControl ItemsSource="{Binding Notes}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Thumb Width="150" Height="150" DragDelta="Thumb_DragDelta" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemContainerStyle>
                <Style>
                    <Setter Property="Canvas.Left" Value="{Binding X}" />
                    <Setter Property="Canvas.Top" Value="{Binding Y}" />
                </Style>
            </ItemsControl.ItemContainerStyle>
        </ItemsControl>
    

    And the codebehind:

     public partial class MainWindow : Window
    {
        public ObservableCollection<Note> Notes { get; set; }
    
        public MainWindow()
        {
            InitializeComponent();
    
            DataContext = this;
    
            Notes = new ObservableCollection<Note>();
            Notes.Add(new Note(){Title="test", X=100, Y=0});
        }
    
        private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            Note n = (Note)((FrameworkElement)sender).DataContext;
            n.X += e.HorizontalChange;
            n.Y += e.VerticalChange;
        }
    }
    
    public class Note : INotifyPropertyChanged
    {
        private string title;
        private double x;
        private double y;
    
        public double Y
        {
            get { return y; }
            set
            {
                y = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Y"));
            }
        }
    
        public double X
        {
            get { return x; }
            set
            {
                x = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("X"));
            }
        }
    
    
        public string Title
        {
            get { return title; }
            set
            {
                title = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Title"));
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to implement a button in my app, that quits
Basically, I want a class to be able to implement two different versions of
I want to implement Scrum, but I can't decide on a Sprint length. Ken
Related to my previous question ( found here ), I want to be able
I am looking at extending an existing application through the use of a plugin
Ok I have a table in my SQL Server database that stores comments. My
I'm thinking about starting work on a fairly large scale .NET or ASP.NET project
I note that some sites (such as gmail) allow the user to authenticate over
We got a project where we're supposed to let 3rd party developers create their

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.