Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. default control template xaml

default control template xaml

Scheduled Pinned Locked Moved WPF
wpfcsharpquestion
6 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bob Bedell
    wrote on last edited by
    #1

    Does anyone know where to find the complete default control template xaml listings for the WPF controls? Thanks. Bob

    K 1 Reply Last reply
    0
    • B Bob Bedell

      Does anyone know where to find the complete default control template xaml listings for the WPF controls? Thanks. Bob

      K Offline
      K Offline
      Kamal Gurnani
      wrote on last edited by
      #2

      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;

      B 4 Replies Last reply
      0
      • K Kamal Gurnani

        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;

        B Offline
        B Offline
        Bob Bedell
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • K Kamal Gurnani

          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;

          B Offline
          B Offline
          Bob Bedell
          wrote on last edited by
          #4

          The error is obviously thrown by the line: XamlWriter.Save(template, writer); since template is null.

          1 Reply Last reply
          0
          • K Kamal Gurnani

            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;

            B Offline
            B Offline
            Bob Bedell
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • K Kamal Gurnani

              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;

              B Offline
              B Offline
              Bob Bedell
              wrote on last edited by
              #6

              Turns out, after a little more exploration, that the Template property will be null unless the control has been rendered on the screen.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups