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

  • Home
  • SEARCH
  • 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 222919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:10:20+00:00 2026-05-11T19:10:20+00:00

I am trying to use the MVVM pattern for the first time. So I

  • 0

I am trying to use the MVVM pattern for the first time. So I have an ItemsControl filled with my viewmodel objects, displayed using DataTemplate‘s; the objects are “nodes” and “edges” represented in DataTemplate‘s with Thumb and Polyline objects and I want to be able to detect clicks and drags on the ItemsControl in order to move the nodes and edges.

Two questions:

  • How do I attach mouse event handlers to the Polyline‘s and Thumb‘s to be handled by the little viewmodels? (I could attach a Thumb.DragDelta handler to the ItemsControl and e.OriginalSource points to the Thumb, but how do I get the corresponding viewmodel object?)
  • How do I attach mouse event handlers to the ItemsControl to detect mouse clicks and drags on blank space? (answer is below)

Note: I know it might not be considered a proper ViewModel if it’s directly handling events of the View. But the important point is, I need to handle mouse events and I am not sure how to attach them.

  • 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-11T19:10:20+00:00Added an answer on May 11, 2026 at 7:10 pm

    I figured out the answer to the second question. I needed an ItemsControl that supported scrolling, and I needed to have the items on a Grid rather than the default StackPanel. To fulfill both requirements, I used a ControlTemplate:

    <!--In the resources...-->
    <ControlTemplate x:Key="GraphTemplate" TargetType="ItemsControl">
        <ScrollViewer Name="ScrollViewer"
                      Padding="{TemplateBinding Padding}"
                      HorizontalScrollBarVisibility="Auto">
            ...
                <Grid Name="Panel" IsItemsHost="True"
                      Background="{TemplateBinding ItemsControl.Background}"/>
            ...
        </ScrollViewer>
    </ControlTemplate>
    <!--Later...-->
    <ItemsControl x:Name="_itemsControl" 
                  ItemsSource="{Binding Items}"
                  Template="{StaticResource GraphTemplate}"
                  Background="LightYellow"/>
    

    In order to get mouse events with meaningful mouse coordinates (i.e. coordinates in scrollable space), it was necessary to obtain a reference to the grid using a strange incantation:

    Grid grid = (Grid)_itemsControl.Template.FindName("Panel", _itemsControl);
    

    Then you attach event handlers to the grid, and inside the mouse event handlers, get the mouse coordinates w.r.t. the grid using

    Point p = e.GetPosition((IInputElement)sender);
    

    In order to get mouse events on the entire surface, the control (actually the grid) must have a background, so I set Background=”LightYellow” above, which propagates to the grid via a binding in the ControlTemplate.

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

Sidebar

Ask A Question

Stats

  • Questions 162k
  • Answers 162k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer EDIT: Firefox has a registry key in HKLM\Software\Mozilla\Mozilla Firefox\\Main\InstallDirectory key.… May 12, 2026 at 12:03 pm
  • Editorial Team
    Editorial Team added an answer Cmd+option double-click works for me. May 12, 2026 at 12:03 pm
  • Editorial Team
    Editorial Team added an answer Have you tried to namespace it like this: http://hasseg.org/blog/?p=165 So… May 12, 2026 at 12:03 pm

Related Questions

I am knocking together a WPF demo for our department at work to show
I have been trying to come up with a better localization approach than LocBaml
I am trying to use the InternalsVisibleTo assembly attribute to make my internal classes
I am trying to use the BGGA closures prototype with an existing JDK 6

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.