none
Hit Testing in MVVM, from ViewModel

    Question

  • Hello all.

    How can I go about performing a HitTest on a View, from the associated ViewModel?

    I have a ViewModel that is controlling a secondary pointer, which can move around the associated View.  Under certain conditions I want my ViewModel to trigger that a "click" has been performed, and would like to send that "click" through to my View; or would like to detect what is there so I can perform certain actions in the ViewModel and trigger Bindings so the visual objects alter their look to correspond to that "click".

    Thanks for any help!

    Friday, October 08, 2010 10:22 PM

Answers

  • You can do this using normal hit testing, but I would consider changing your design.  It might be nicer to make a control that can perform the hit testing for you, and use it to track the UIElement under the cursor.  Your VM could bind to a DP on this control, and style much more MVVM pure...

     


    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, October 08, 2010 10:28 PM

All replies

  • You can do this using normal hit testing, but I would consider changing your design.  It might be nicer to make a control that can perform the hit testing for you, and use it to track the UIElement under the cursor.  Your VM could bind to a DP on this control, and style much more MVVM pure...

     


    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, October 08, 2010 10:28 PM
  • Hi Evil Closet Monkey,

    You can use attached behavior to execute behaviors on the view from View Model. I have discussed it here:
    http://shujaatsiddiqi.blogspot.com/2010/08/attached-behaviors.html

    Thanks,
    Muhammad

    Friday, October 08, 2010 10:30 PM
  • Thanks Reed.

    I already have a unique control to represent the pointer -- I hadn't thought to use it to perform the hit test.  I'll dig into the code and see what I can come up with there.

    In this case, would you see the pointer (the control performing the hit test) interacting directly with the other controls on the View, which would then send notification back to the ViewModel, or would the pointer communicate back tot he ViewModel and have it trigger changes to the View via DataTriggers?

    Thanks again for the tips!

    Friday, October 08, 2010 10:52 PM
  • In this case, would you see the pointer (the control performing the hit test) interacting directly with the other controls on the View, which would then send notification back to the ViewModel, or would the pointer communicate back tot he ViewModel and have it trigger changes to the View via DataTriggers?

     

    You could go either way.  Since it's a custom control, I'd just have it expose a DependencyProperty for the UIElement under it.  The VM could bind straight to that, if you wanted, and as you move the cursor around, it could perform the hit testing, and always keep the VM aware of it's current element.

     

    Alternatively, you might be able to use the accessibility model exposed by the Windows Automation API to handle this entirely in your control - it could trigger the "click" events, etc, of the UIElement, without the VM being part of the equation at all.


    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, October 08, 2010 10:59 PM