Faerdan
Topic Author
Posts: 55
Joined: 02 Oct 2015, 09:11
Location: Galway, Ireland
Contact:

Accessing ItemsControl Items in C# (Unity)

19 Jan 2016, 04:48

Hi there,

I need to iterate through the Items of an ItemsControl in C# (as UIElements, like Panel Children).

All of the examples I have found for WPF use a variation of this code:
for(int i = 0; i < itemsControl.Items.Count; i++)
{
    UIElement uiElement = (UIElement)itemsControl.ItemContainerGenerator.ContainerFromIndex(i);
}
But ItemContainerGenerator.ContainerFromIndex does not exist in the Noesis ItemsControl class.

What is the best way of doing this in Noesis?


Thanks,

Mark
 
User avatar
sfernandez
Site Admin
Posts: 1918
Joined: 22 Dec 2011, 19:20

Re: Accessing ItemsControl Items in C# (Unity)

19 Jan 2016, 06:41

Hi,

We are working to implement ItemContainerGenerator for next version.

Meanwhile, the solution is to provide access to the Panel that holds the item containers. You will need to name the ItemsPresenter or directly define and name the Panel you want to use as ItemsHost in the control template:
<ControlTemplate x:Key="CustomItemsControlTemplate" TargetType="ItemsControl">
  <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}">
    <StackPanel x:Name="itemsPanel" IsItemsHost="True"/>
  </Border>
</ControlTemplate> 
void IterateItemsControl(ItemsControl itemsControl)
{
  StackPanel panel = (StackPanel)itemsControl.GetTemplatChild("itemsPanel");
  for(int i = 0; i < itemsControl.Items.Count; i++)
  {
    UIElement container = (UIElement)panel.Children[i];
  }
} 
 
Faerdan
Topic Author
Posts: 55
Joined: 02 Oct 2015, 09:11
Location: Galway, Ireland
Contact:

Re: Accessing ItemsControl Items in C# (Unity)

19 Jan 2016, 14:04

Cool, thank you.

Who is online

Users browsing this forum: No registered users and 1 guest