The LogicalChildren of a ContentControl is of ‘protected-type’ access. You can derive a custom class from it and then expose a public version of the ‘LogicalChildren’ property.
[C#]
publicclassSPanel : StackPanel
{
public System.Collections.IEnumerator LogicalChildren {
get {
returnbase.LogicalChildren;
}
}
}
You can create a template programatically by populating the VisualTree of the template.
[C#]
DataTemplate template = new DataTemplate();
template.VisualTree = new FrameworkElementFactory(typeof(Path));
template.VisualTree.SetBinding(Path.StrokeProperty, new Binding('Stroke'));
template.VisualTree.SetBinding(Path.StrokeThicknessProperty, new Binding('StrokeThickness'));
template.VisualTree.SetBinding(Path.FillProperty, new Binding('Interior'));
template.VisualTree.SetBinding(Path.DataProperty, new Binding('Geometry'));
But, if want to get hold of an item in a DataTemplate (which gets applied for each item in the ItemsControl), then there is more code involved as explained here:
ControlTemplate can be created for a HeaderedItemsControl as follows.
[XAML]
<!--Define a control template for a HeaderedItemsControl--><StyleTargetType='HeaderedItemsControl'><SetterProperty='Template'><Setter.Value><ControlTemplateTargetType='{x:Type HeaderedItemsControl}'><StackPanel><Grid><RectangleFill='{TemplateBinding Background}'/><ContentPresenterContentSource='Header'/></Grid><Grid><RectangleStroke='{TemplateBinding BorderBrush}'/><ItemsPresenterMargin='2,0,0,0'/></Grid></StackPanel></ControlTemplate></Setter.Value></Setter></Style>
...
<HeaderedItemsControlxmlns:sys='clr-namespace:System;assembly=mscorlib'Header='My colors'Background='SteelBlue'BorderBrush='DarkSlateBlue'><sys:String>Red</sys:String><sys:String>Yellow</sys:String><sys:String>Blue</sys:String><sys:String>Green</sys:String></HeaderedItemsControl>
The example Extensible Application Markup Language (XAML) version could use the <Button.Content> tags around the content of each button, but it is not necessary. The Four buttons can be created as follows.
[XAML]
<!--Create a Button with a string as its content.--><Button>This is string content of a Button</Button><!--Create a Button with a DateTime object as its content.--><Buttonxmlns:sys='clr-namespace:System;assembly=mscorlib'><sys:DateTime>2004/3/4 13:6:55</sys:DateTime></Button><!--Create a Button with a single UIElement as its content.--><Button><RectangleHeight='40'Width='40'Fill='Blue'/></Button><!--Create a Button with a panel that contains multiple objects
as its content.--><Button><StackPanel><EllipseHeight='40'Width='40'Fill='Blue'/><TextBlockTextAlignment='Center'>Button</TextBlock></StackPanel></Button>