Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

I would say your best bet would be to add the MouseDragElementBehavior to your custom ItemsControl in code instead of in the Xaml. Here is how this might look (using a Grid since that is easier to demo):

public class DraggableGrid : Grid
{

    public DraggableGrid()
    {
        Loaded += new RoutedEventHandler(DraggableGrid_Loaded);
    }

    void DraggableGrid_Loaded(object sender, RoutedEventArgs e)
    {
        MouseDragElementBehavior dragable = new MouseDragElementBehavior();
        Interaction.GetBehaviors(this).Add(dragable);
        dragable.Dragging += new MouseEventHandler(dragable_Dragging);
    }    

    void dragable_Dragging(object sender, MouseEventArgs e)
    {
        // Custom Code Here
    }
}

In the section that says Custom Code Here you would loop through you Items and notify them that they are being dragged.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top