eran otzap
eran otzap

Reputation: 12533

get itemscontrol reference from item

i need to drag items between several itemcontrols , each are bound to it's own collection when i drag an item i need to know which itemscontrol it was originally dragged from .

the draggable item template :

  <DataTemplate>
        <Ellipse MouseDown="Ellipse_MouseDown" ></Ellipse>
  </DataTemplate> 

the itemscontrols :

  <ItemsControl Name="Pipe23" ItemsSource="{Binding Path=Pipes[23].Checkers}" ItemTemplate="{StaticResource PipeDataItem}"/>
  <ItemsControl Name="Pipe22" ItemsSource="{Binding Path=Pipes[22].Checkers}" ItemTemplate="{StaticResource PipeDataItem}"/>
  <ItemsControl Name="Pipe21" ItemsSource="{Binding Path=Pipes[21].Checkers}" ItemTemplate="{StaticResource PipeDataItem}"/>
  <ItemsControl Name="Pipe20" ItemsSource="{Binding Path=Pipes[20].Checkers}"  ItemTemplate="{StaticResource PipeDataItem}"/>

when dragging an item on the MouseDown event i can reference the item being dragged but i also need to reference the itemscontrol it was dragged from : how can this be done ?

    private void Ellipse_MouseDown(object sender, MouseButtonEventArgs e)
    {
        Ellipse ellipse = (Ellipse)sender;
        Checker checker = (Checker)ellipse.DataContext;
        //  how do i reference the itemsconrtol containing the current ellipse (item)    
    }

Upvotes: 0

Views: 1642

Answers (1)

Rachel
Rachel

Reputation: 132558

I would navigate up the VisualTree until I find an ItemsControl object, and that would be the parent.

I have some VisualTree helpers posted on my blog that does this, and I could use them like this to find the parent ItemsControl:

private void Ellipse_MouseDown(object sender, MouseButtonEventArgs e)
{
    Ellipse ellipse = (Ellipse)sender;
    Checker checker = (Checker)ellipse.DataContext;

    ItemsControl parent = VisualTreeHelpers.FindAncestor<ItemsControl>(ellipse); 
}

Upvotes: 1

Related Questions