How does WPF obtain the entry control based on data items-ItemContainerGenerator
I. Method:
ContainerFromIndex: return the container of the items in the specified index in ItemCollection.
ContainerFromItem: return the container (entry control such as ComboxItem) corresponding to the specified item ).
Equals (Object): determines whether the specified Object is equal to the current Object.
Finalize: allows objects to try to release resources and clean up objects before they are recycled.
GeneratorPositionFromIndex: gets the position where the item is generated at the specified index.
GetHashCode: Used as a hash function of a specific type.
GetType: Get the Type of the current instance.
IndexFromContainer: gets the index of an item with the specified generated container.
IndexFromGeneratorPosition: returns the index mapped to the specified GereratorPosition.
ItemFromContainer: returns the corresponding item generated by the specified container.
Ii. Events
ItemsChanged: occurs when items in the set change.
3. Note
Note the association between the ItemContainerGenerator class maintenance item Control and Its item container. If a control has an associated ItemContainerGenerator, it can be retrieved through its properties.
You can use ItemContainerGenerator and index-based items to retrieve items or specify data items to retrieve containers. For example, if you have a Combox bound to data and want to obtain a ComboxItem based on its index, you can use the ContainerFromIndex method. To retrieve data items, use the ItemFromContainer method.
Iv. Example:
C #:
public MyTest(){ InitializeComponent(); string[] myCollection= new string[]{"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}; myCombox.DataContext = myCollection; }
static int count = 1;
private void Button_Click(object sender, RoutedEventArgs e)
{
ConmboxItem item = (ComboxItem)
myCombox.ItemContainerGenerator.ContainerFromIndex(3);
item.IsExpanded = true; if (count < 5) { item.Items.Add("Child " + count.ToString()); count++; }
}
Xaml:
<StackPanel x:Name="LayoutRoot" Background="White"> <Combox x:Name="myCombox" Width="200" ItemsSource="{Binding}" Margin="5"/> <Button Content="Add Child to Item 4" Width="150" Click="Button_Click"/></StackPanel>