" /> " /> "/>

access control within items control

90 Views Asked by At

this is my itemsControl :

    <ItemsControl x:Name="MessagesContainer">
         <ItemsControl.ItemsPanel>
               <ItemsPanelTemplate>
                   <StackPanel Orientation="Vertical"></StackPanel>
               </ItemsPanelTemplate>
         </ItemsControl.ItemsPanel>
         <ItemsControl.ItemTemplate>
               <DataTemplate>
                   <StackPanel>
                        <TextBlock Text="{Binding MessageText}" />
                        <TextBlock Text="{Binding MessageCreatedDate}" />
                    </StackPanel>
               </DataTemplate>
         </ItemsControl.ItemTemplate>
    </ItemsControl>

i want to access top 3 items (ie. top 3 stackPanels) to change their background color, so i tried ItemContainerGenerator below :

for (int i = 0; i < 3; i++) 
{
   UIElement uiElement = 
    (UIElement)MessagesContainer.ItemContainerGenerator.ContainerFromIndex(i);//<-- always null
}

but uiElement always having null value Whereas my itemsControl binding well
any ideas ? or any other way to get specific items from itemsControl ?

0

There are 0 best solutions below