Search code examples
silverlighteventssilverlight-4.0behaviorattachedbehaviors

SL4: need to register for a move (or redraw) event on an Item in an ItemsControl


Not finding a move event or redraw event in the FrameworkElement class. And Google not helping either. So...

I have a custom ItemsControl populated by an observable collection in the VM. The ItemsControl itself leverages the

<i:Interaction.Behaviors>
    <ei:MouseDragElementBehavior ConstrainToParentBounds="True"/>
</i:Interaction.Behaviors>

behavior so the user can drag around the whole assembly.

When the user moves the assembly, I want to be notified by each item as the item is repositioned as a result of the assembly moving. So far I have tried registering for

this.myItem.LayoutUpdated += this.OnSomethingNeedsToUpdate;

but it doesn't seem to fire as I drag the assembly around.

Also

this.myItem.MouseMove += this.OnSomethingNeedsToUpdate;

only works if I mouse into the item which is not good enough. Because I am moving the ItemsControl and then have to go mouse into the item to get the event to fire.

Any ideas? Can I look to some ancestor in the visual tree for help in the form of a OneOfMyDecendantsWasRedrawn event or similar? Again I am trying to be notified when an item moves not be notified when the assembly moves.


Solution

  • I ended up writting another behavior for the individual items I care about and then wrote a LINQ query to search up the visual tree looking for ancestors with the MouseDragElementBehavior attached to them. That query found the ItemsControl since it was an eventual parent of the Item. I was then able to register for the Dragging event as desried.

    Thanks again to Bryant for providing the solution over here.