Ather Mohiuddin
Ather Mohiuddin

Reputation: 23

retrieve usercontrol from itemscontrol item

i'm new to wpf and i am having a problem with items control. What i want to do is that i want to retrieve the user control that i've added in itemtemplate of items control. I tried using LoadContent() method of DataTemplate but it returns me the default template.

Here my code

ItemsControl parent = FindParent<ItemsControl>( this );


  //this.isEditMode = true;
  //this.editIngLayer.Visibility = Visibility.Visible;


  foreach( var container in parent.Items )
  { 
    DependencyObject contentPresenter=
        parent.ItemContainerGenerator.ContainerFromItem( container ) as ContentPresenter;


    //Something to retrieve the usercontrol
    MyUserControl uC=contentPresenter.GetControl();
    //
  }

Thanks.

Upvotes: 1

Views: 84

Answers (1)

Nitin Purohit
Nitin Purohit

Reputation: 18578

If you have your ItemsControl item with you then you can iterate its Visualtree to reach to your usercontrol using VisualTreeHelper

Recursive find child is explained in this post How can I find WPF controls by name or type?

Upvotes: 1

Related Questions