Page 1 of 1

Accessing ItemsControl Items in C# (Unity)

Posted: 19 Jan 2016, 04:48
by Faerdan
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

Re: Accessing ItemsControl Items in C# (Unity)

Posted: 19 Jan 2016, 06:41
by sfernandez
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];
  }
} 

Re: Accessing ItemsControl Items in C# (Unity)

Posted: 19 Jan 2016, 14:04
by Faerdan
Cool, thank you.