DataTemplate.LoadContent Method
Creates the UIElement objects in the DataTemplate.
Namespace: System.Windows
Assembly: System.Windows (in System.Windows.dll)
When you call LoadContent, the UIElement objects in the DataTemplate are created, and you can add them to the visual tree of another UIElement.
The following examples demonstrate using the LoadContent method to change the appearance of a Border at run time. The example creates a ListBox that contains the numbers 1 through 10. When the user selects an item in the ListBox, the Border displays the selected number. If the user selects an even number, the number is red and has a green circle around it. If the user selects an odd number, the number is blue and has a purple square around it.
<StackPanel Background="White" Name="rootStackPanel"> <StackPanel.Resources> <DataTemplate x:Key="oddNumberTemplate"> <Grid> <Rectangle Stroke="Purple" StrokeThickness="4" /> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" Foreground="Blue" FontWeight="Bold"/> </Grid> </DataTemplate> <DataTemplate x:Key="evenNumberTemplate"> <Grid> <Ellipse Stroke="Green" StrokeThickness="4"/> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" Foreground="Red" FontWeight="Bold" /> </Grid> </DataTemplate> </StackPanel.Resources> <Border Name="selectedItemDisplay" Width="50" Height="50"/> <ListBox Name="numberList" SelectionChanged="ListBox_SelectionChanged"> <ListBoxItem Content="1"/> <ListBoxItem Content="2"/> <ListBoxItem Content="3"/> <ListBoxItem Content="4"/> <ListBoxItem Content="5"/> <ListBoxItem Content="6"/> <ListBoxItem Content="7"/> <ListBoxItem Content="8"/> <ListBoxItem Content="9"/> <ListBoxItem Content="10"/> </ListBox> </StackPanel>
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem); SelectDataTemplate(lbi.Content); } private void SelectDataTemplate(object value) { string numberStr = value as string; if (numberStr != null) { int num; try { num = Convert.ToInt32(numberStr); } catch { return; } DataTemplate template; // Select one of the DataTemplate objects, based on the // value of the selected item in the ComboBox. if (num % 2 != 0) { template = rootStackPanel.Resources["oddNumberTemplate"] as DataTemplate; } else { template = rootStackPanel.Resources["evenNumberTemplate"] as DataTemplate; } selectedItemDisplay.Child = template.LoadContent() as UIElement; TextBlock tb = FindVisualChild<TextBlock>(selectedItemDisplay); tb.Text = numberStr; } } private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { DependencyObject child = VisualTreeHelper.GetChild(obj, i); if (child != null && child is childItem) return (childItem)child; else { childItem childOfChild = FindVisualChild<childItem>(child); if (childOfChild != null) return childOfChild; } } return null; }
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
I can say that the one who wrote it is not so familiar with VB, since && operator is AndAlso in VB, not And.
I was only reviewing the FindVisualChild function so I have no clue what about the rest.
<Extension()> _
Public Function GetVisualDescendant(Of TChild As DependencyObject)(ByVal reference As DependencyObject) As TChild
For i = 0 To VisualTreeHelper.GetChildrenCount(reference) - 1
Dim child = VisualTreeHelper.GetChild(reference, i)
If child Is Nothing Then
Continue For
ElseIf Type.Equals(child.GetType, GetType(TChild)) Then
Return child
Else
Return GetVisualDescendant(Of TChild)(child)
End If
Next
Return Nothing
End Function
- 12/10/2009
- Shimmy Weitzhandler
- 12/10/2009
- Shimmy Weitzhandler
<Extension()> _
Public Function GetVisualAncestor(Of TParent As DependencyObject)(ByVal reference As DependencyObject) As TParent
Dim parent = VisualTreeHelper.GetParent(reference)
While parent IsNot Nothing AndAlso Not Type.Equals(parent.GetType, GetType(TParent))
parent = GetVisualAncestor(Of TParent)(parent)
End While
Return parent
End Function
- 12/10/2009
- Shimmy Weitzhandler