locked
Is there any how to make control without xaml ? RRS feed

  • Question

  • Is there any how to make control without Generic.xaml ?

    When we use templete control , Normally we made a custom control using  geniri.xaml  to make make up control templet.

     

    without Generic.xaml  is it possiable to make up   control templet  that contained many component like a button stackpannel.

    this is my sample code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Windows.UI;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Documents;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    
    
    namespace derrick_test
    {
    	public  class DerrickPanel : Control
    	{
    		private StackPanel parentPanel;
    		private TextBlock testBlock;
    		
    
    		public DerrickPanel()
    		{
    			this.DefaultStyleKey = typeof(DerrickPanel);
    
    			parentPanel = new StackPanel();
    			testBlock = new TextBlock();
    			parentPanel.Orientation = Orientation.Vertical;
    			parentPanel.Background = new SolidColorBrush(Colors.Red);
    			testBlock.Foreground = new SolidColorBrush(Colors.Aqua);
    			testBlock.Text = " hello World ";
    			parentPanel.Children.Add(testBlock);
    
    		}
    
    	}
    }
    

      Best Regrads Derrick.

    Tuesday, January 20, 2015 2:05 AM

Answers

  • Here's one solution that I found:

    public sealed partial class MainPage : Page
    {
      public MainPage()
      {
        this.InitializeComponent();
      }
    
      protected override void OnNavigatedTo(NavigationEventArgs e)
      {
        DerrickPanel D = new DerrickPanel();
        MyGrid.Children.Add(D);
      }
    }
    
    
    public class DerrickPanel : Control
    {
      private StackPanel parentPanel;
      private TextBlock testBlock;
    
      public DerrickPanel()
      {
        this.DefaultStyleKey = typeof(DerrickPanel);
        this.Template = DerrickPanel.CreateTemplate();
      }
    
         
      private static ControlTemplate CreateTemplate()
      {
        string xaml = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">";
        xaml += "<StackPanel Orientation=\"Vertical\" Background=\"Red\">";
        xaml += "<TextBlock Foreground=\"Aqua\">Hello World</TextBlock>";
        xaml += "</StackPanel>";
        xaml += "</ControlTemplate>";
        var сt = (ControlTemplate)XamlReader.Load(xaml);
        return сt;
      }
    }


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, January 22, 2015 2:08 PM
    Moderator

All replies

  • I've had this question before, but never had a answer for it.  I'll ask again.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, January 20, 2015 2:59 PM
    Moderator
  • Here's one solution that I found:

    public sealed partial class MainPage : Page
    {
      public MainPage()
      {
        this.InitializeComponent();
      }
    
      protected override void OnNavigatedTo(NavigationEventArgs e)
      {
        DerrickPanel D = new DerrickPanel();
        MyGrid.Children.Add(D);
      }
    }
    
    
    public class DerrickPanel : Control
    {
      private StackPanel parentPanel;
      private TextBlock testBlock;
    
      public DerrickPanel()
      {
        this.DefaultStyleKey = typeof(DerrickPanel);
        this.Template = DerrickPanel.CreateTemplate();
      }
    
         
      private static ControlTemplate CreateTemplate()
      {
        string xaml = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">";
        xaml += "<StackPanel Orientation=\"Vertical\" Background=\"Red\">";
        xaml += "<TextBlock Foreground=\"Aqua\">Hello World</TextBlock>";
        xaml += "</StackPanel>";
        xaml += "</ControlTemplate>";
        var сt = (ControlTemplate)XamlReader.Load(xaml);
        return сt;
      }
    }


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, January 22, 2015 2:08 PM
    Moderator
  • If your custom control is simply made up off other controls, like StackPanels and TextBlocks, like the DerrickPanel class in the code you have posted you should create a UserControl.

    Then there is no need to define a control template at all but just define the layout of the control as the Content of the UserControl itself.

    Please remember to mark helpful posts as answer and/or helpful.

    Thursday, January 22, 2015 4:02 PM