Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
swdev:dotnet:wpf:datatemplates [2012/11/14 13:00] smayr created |
swdev:dotnet:wpf:datatemplates [2012/12/07 17:15] (current) smayr [Example 2] |
||
---|---|---|---|
Line 24: | Line 24: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | == How to: Find DataTemplate-Generated Elements == | ||
+ | |||
+ | To find elements generated by a DataTemplate: | ||
+ | <code xml> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | <Binding XPath=" | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | ... | ||
+ | <ListBox Name=" | ||
+ | | ||
+ | < | ||
+ | <Binding Source=" | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | We have to find the ContentPresenter first: | ||
+ | <code csharp> | ||
+ | // Getting the currently selected ListBoxItem | ||
+ | // Note that the ListBox must have | ||
+ | // IsSynchronizedWithCurrentItem set to True for this to work | ||
+ | ListBoxItem myListBoxItem = | ||
+ | (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem)); | ||
+ | |||
+ | // Getting the ContentPresenter of myListBoxItem | ||
+ | ContentPresenter myContentPresenter = FindVisualChild< | ||
+ | |||
+ | // Finding textBlock from the DataTemplate that is set on that ContentPresenter | ||
+ | DataTemplate myDataTemplate = myContentPresenter.ContentTemplate; | ||
+ | TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName(" | ||
+ | |||
+ | // Do something to the DataTemplate-generated TextBlock | ||
+ | MessageBox.Show(" | ||
+ | + myTextBlock.Text); | ||
+ | . . . | ||
+ | private childItem FindVisualChild< | ||
+ | where childItem : DependencyObject | ||
+ | { | ||
+ | for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); | ||
+ | { | ||
+ | DependencyObject child = VisualTreeHelper.GetChild(obj, | ||
+ | if (child != null && child is childItem) | ||
+ | return (childItem)child; | ||
+ | else | ||
+ | { | ||
+ | childItem childOfChild = FindVisualChild< | ||
+ | if (childOfChild != null) | ||
+ | return childOfChild; | ||
+ | } | ||
+ | } | ||
+ | return null; | ||
+ | } | ||
+ | </ | ||
+ | Source: [[http:// | ||
+ | |||
+ | === Example 1 === | ||
+ | For a ListBoxItem: | ||
+ | <code xml> | ||
+ | <ListBox Name=" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | <code csharp> | ||
+ | ListBoxItem lbi = (ListBoxItem)listBox.ItemContainerGenerator.ContainerFromIndex(IndexInListBox); | ||
+ | ComboBox cb = (ComboBox)listBox.ItemTemplate.FindName(" | ||
+ | </ | ||
+ | |||
+ | === Example 2 === | ||
+ | For a ListBoxItem: | ||
+ | <code xml> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | . . . | ||
+ | <ListBox ItemsSource=" | ||
+ | </ | ||
+ | |||
+ | <code csharp> | ||
+ | // To get GreekGod object | ||
+ | GreekGod greekGod = (GreekGod)(listBox.Items[0]); | ||
+ | |||
+ | // To get ListBoxItem container holding object GreekGod | ||
+ | ListBoxItem lbi1 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromIndex(0)); | ||
+ | ListBoxItem lbi2 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromItem(listBox.Items.CurrentItem)); | ||
+ | </ | ||
+ | |||
+ | Source: [[http:// |