default control template xaml
-
Does anyone know where to find the complete default control template xaml listings for the WPF controls? Thanks. Bob
-
Does anyone know where to find the complete default control template xaml listings for the WPF controls? Thanks. Bob
The WPF documentation doesn’t list the XAML for standard control templates. But, you can write program to get the control template and then modify it and Re-Apply it. Here is the program to get the control Template Markup of a given control. In a Main window take a listbox control to enlist all the controls, and when any item is selected the corrosponding Control Template's Markup is displayed in a Textblock control. Here is the WPF program to demonstrate the same This is the XAML part <Window x:Class="DefaultControlTemplate.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="462" Width="572" Loaded="Window_Loaded" > <Grid> <ListBox Margin="12,12,0,12" Name="listBox1" HorizontalAlignment="Left" Width="168" SelectionChanged="listBox1_SelectionChanged" /> <TextBlock HorizontalAlignment="Right" Margin="0,14,12,10" Name="textBlock1" Width="159" /> </Grid> </Window> This is how .cs file would look like private void Window_Loaded(object sender, RoutedEventArgs e) { Type controlType = typeof(Control); List<Type> ctrlTypes = new List<Type>(); //searches all the types in a dll where generic control class is defined Assembly assembly = Assembly.GetAssembly(typeof(Control)); foreach (Type type in assembly.GetTypes()) { // Only add a type of the list if it's a Control, a concrete class, // and public. if (type.IsSubclassOf(controlType) && !type.IsAbstract && type.IsPublic) { ctrlTypes.Add(type); } } listBox1.ItemsSource = ctrlTypes; } ------------------------------------------------------------ private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { // Get the selected type. Type type = (Type)listBox1.SelectedItem; // Instantiate the type. ConstructorInfo info = type.GetConstructor(System.Type.EmptyTypes); Control control = (Control)info.Invoke(null); // Get the template. ControlTemplate template = control.Template;
-
The WPF documentation doesn’t list the XAML for standard control templates. But, you can write program to get the control template and then modify it and Re-Apply it. Here is the program to get the control Template Markup of a given control. In a Main window take a listbox control to enlist all the controls, and when any item is selected the corrosponding Control Template's Markup is displayed in a Textblock control. Here is the WPF program to demonstrate the same This is the XAML part <Window x:Class="DefaultControlTemplate.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="462" Width="572" Loaded="Window_Loaded" > <Grid> <ListBox Margin="12,12,0,12" Name="listBox1" HorizontalAlignment="Left" Width="168" SelectionChanged="listBox1_SelectionChanged" /> <TextBlock HorizontalAlignment="Right" Margin="0,14,12,10" Name="textBlock1" Width="159" /> </Grid> </Window> This is how .cs file would look like private void Window_Loaded(object sender, RoutedEventArgs e) { Type controlType = typeof(Control); List<Type> ctrlTypes = new List<Type>(); //searches all the types in a dll where generic control class is defined Assembly assembly = Assembly.GetAssembly(typeof(Control)); foreach (Type type in assembly.GetTypes()) { // Only add a type of the list if it's a Control, a concrete class, // and public. if (type.IsSubclassOf(controlType) && !type.IsAbstract && type.IsPublic) { ctrlTypes.Add(type); } } listBox1.ItemsSource = ctrlTypes; } ------------------------------------------------------------ private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { // Get the selected type. Type type = (Type)listBox1.SelectedItem; // Instantiate the type. ConstructorInfo info = type.GetConstructor(System.Type.EmptyTypes); Control control = (Control)info.Invoke(null); // Get the template. ControlTemplate template = control.Template;
I ran your first version which looks promising, only the line: ControlTemplate template = control.Template; Always assigns a null value to the template variable. The error message is typically: << Error generating template: Parameter name: obj>> Are you seeing the same thing? Thanks for your reply, Bob
-
The WPF documentation doesn’t list the XAML for standard control templates. But, you can write program to get the control template and then modify it and Re-Apply it. Here is the program to get the control Template Markup of a given control. In a Main window take a listbox control to enlist all the controls, and when any item is selected the corrosponding Control Template's Markup is displayed in a Textblock control. Here is the WPF program to demonstrate the same This is the XAML part <Window x:Class="DefaultControlTemplate.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="462" Width="572" Loaded="Window_Loaded" > <Grid> <ListBox Margin="12,12,0,12" Name="listBox1" HorizontalAlignment="Left" Width="168" SelectionChanged="listBox1_SelectionChanged" /> <TextBlock HorizontalAlignment="Right" Margin="0,14,12,10" Name="textBlock1" Width="159" /> </Grid> </Window> This is how .cs file would look like private void Window_Loaded(object sender, RoutedEventArgs e) { Type controlType = typeof(Control); List<Type> ctrlTypes = new List<Type>(); //searches all the types in a dll where generic control class is defined Assembly assembly = Assembly.GetAssembly(typeof(Control)); foreach (Type type in assembly.GetTypes()) { // Only add a type of the list if it's a Control, a concrete class, // and public. if (type.IsSubclassOf(controlType) && !type.IsAbstract && type.IsPublic) { ctrlTypes.Add(type); } } listBox1.ItemsSource = ctrlTypes; } ------------------------------------------------------------ private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { // Get the selected type. Type type = (Type)listBox1.SelectedItem; // Instantiate the type. ConstructorInfo info = type.GetConstructor(System.Type.EmptyTypes); Control control = (Control)info.Invoke(null); // Get the template. ControlTemplate template = control.Template;
The error is obviously thrown by the line: XamlWriter.Save(template, writer); since template is null.
-
The WPF documentation doesn’t list the XAML for standard control templates. But, you can write program to get the control template and then modify it and Re-Apply it. Here is the program to get the control Template Markup of a given control. In a Main window take a listbox control to enlist all the controls, and when any item is selected the corrosponding Control Template's Markup is displayed in a Textblock control. Here is the WPF program to demonstrate the same This is the XAML part <Window x:Class="DefaultControlTemplate.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="462" Width="572" Loaded="Window_Loaded" > <Grid> <ListBox Margin="12,12,0,12" Name="listBox1" HorizontalAlignment="Left" Width="168" SelectionChanged="listBox1_SelectionChanged" /> <TextBlock HorizontalAlignment="Right" Margin="0,14,12,10" Name="textBlock1" Width="159" /> </Grid> </Window> This is how .cs file would look like private void Window_Loaded(object sender, RoutedEventArgs e) { Type controlType = typeof(Control); List<Type> ctrlTypes = new List<Type>(); //searches all the types in a dll where generic control class is defined Assembly assembly = Assembly.GetAssembly(typeof(Control)); foreach (Type type in assembly.GetTypes()) { // Only add a type of the list if it's a Control, a concrete class, // and public. if (type.IsSubclassOf(controlType) && !type.IsAbstract && type.IsPublic) { ctrlTypes.Add(type); } } listBox1.ItemsSource = ctrlTypes; } ------------------------------------------------------------ private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { // Get the selected type. Type type = (Type)listBox1.SelectedItem; // Instantiate the type. ConstructorInfo info = type.GetConstructor(System.Type.EmptyTypes); Control control = (Control)info.Invoke(null); // Get the template. ControlTemplate template = control.Template;
It appears that when you construct a WPF control in code, its Template property isn't initialized. For example: ListBox listBox = new ListBox(); If you instantiate a ListBox like this, then walk the object hierarchy down to the Template property of the Control base class, you'll see that it hasn't been initilaized.
-
The WPF documentation doesn’t list the XAML for standard control templates. But, you can write program to get the control template and then modify it and Re-Apply it. Here is the program to get the control Template Markup of a given control. In a Main window take a listbox control to enlist all the controls, and when any item is selected the corrosponding Control Template's Markup is displayed in a Textblock control. Here is the WPF program to demonstrate the same This is the XAML part <Window x:Class="DefaultControlTemplate.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="462" Width="572" Loaded="Window_Loaded" > <Grid> <ListBox Margin="12,12,0,12" Name="listBox1" HorizontalAlignment="Left" Width="168" SelectionChanged="listBox1_SelectionChanged" /> <TextBlock HorizontalAlignment="Right" Margin="0,14,12,10" Name="textBlock1" Width="159" /> </Grid> </Window> This is how .cs file would look like private void Window_Loaded(object sender, RoutedEventArgs e) { Type controlType = typeof(Control); List<Type> ctrlTypes = new List<Type>(); //searches all the types in a dll where generic control class is defined Assembly assembly = Assembly.GetAssembly(typeof(Control)); foreach (Type type in assembly.GetTypes()) { // Only add a type of the list if it's a Control, a concrete class, // and public. if (type.IsSubclassOf(controlType) && !type.IsAbstract && type.IsPublic) { ctrlTypes.Add(type); } } listBox1.ItemsSource = ctrlTypes; } ------------------------------------------------------------ private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { // Get the selected type. Type type = (Type)listBox1.SelectedItem; // Instantiate the type. ConstructorInfo info = type.GetConstructor(System.Type.EmptyTypes); Control control = (Control)info.Invoke(null); // Get the template. ControlTemplate template = control.Template;
Turns out, after a little more exploration, that the Template property will be null unless the control has been rendered on the screen.