How does WPF obtain the entry control based on data items-ItemContainerGenerator

Source: Internet
Author: User

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>

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.