Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have this Code in Xaml

XML
<ItemsControl Margin="10" DockPanel.Dock="Left"
                 ItemsSource="{Binding SnippetList}">
                       <ItemsControl.ItemTemplate>
                           <DataTemplate>
                               <v:MyUserControl Margin="2"/>
                           </DataTemplate>
                       </ItemsControl.ItemTemplate>
                   </ItemsControl>


How can I access the object of SnippetList which is bound to a MyUserControl from MyUserControl's code behind. I know it is a simple question but please help.

Something like this is what I require
SnippetList is an ObservableCollection of SnippetObject

C#
SnippetObject temp = this.getSnippetObject(); //this refers to MyUserControl


I need objects in SnippetObject to use it from some calculations and not for binding it.
Posted

1 solution

If you want to access your ItemsControl in the UserControl code-behind file, you need to give the ItemsControl a name like:
XML
<itemscontrol x:name="MyItemsControl Items=" path="Items}"" xmlns:x="#unknown" />


Then use this name to access the ItemsControl:

C#
foreach(var item in MyItemsControl.Items)
{

}


If you want to access the ItemsControl from outside of the UserControl you first need to define a property on your UserControl and than use that property to access the ItemsControl.

Uroš
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900