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

老子叫甜甜 提交于 2019-12-13 03:35:46

问题


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.


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/7459066/sl4-need-to-register-for-a-move-or-redraw-event-on-an-item-in-an-itemscontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!