TemplateBinding exampleprivate void Window_Loaded(object sender, RoutedEventArgs e)
{
Type controlType = typeof(Control);
List<Type> derivedTypes = new List<Type>();
Assembly assembly = Assembly.GetAssembly(typeof(Control));
foreach (Type type in assembly.GetTypes())
{
if (type.IsSubclassOf(controlType) && !type.IsAbstract && type.IsPublic)
{
derivedTypes.Add(type);
}
}
derivedTypes.Sort(delegate(Type x, Type y)
{
return x.FullName.CompareTo(y.FullName);
});
lstTypes.ItemsSource = derivedTypes;
}
private void lstTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
Type type = (Type)lstTypes.SelectedItem;
ConstructorInfo info = type.GetConstructor(Type.EmptyTypes);
Control control = (Control)info.Invoke(null);
control.Visibility = Visibility.Collapsed;
grid.Children.Add(control);
ControlTemplate template = control.Template;
XmlWriterSettings setting = new XmlWriterSettings();
setting.Indent = true;
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb, setting);
System.Windows.Markup.XamlWriter.Save(template, writer);
txtTemplate.Text= sb.ToString();
grid.Children.Remove(control);
}
catch (Exception ex)
{
txtTemplate.Text = string.Format("Error generating tempalte:{0}",ex);
}
}