none
How to define ContentControl's ContentTemplate in code behind c#?

    Question

  • Hi,

    Im trying to define a ContentControl in code behind for my Popup control and trying to set its ContentTemplate in code behind. I know it should be done in XAML, but its a requirement in code behind.. so can anyone suggest how to do it in codebehind?

    Code:

     ContentControl contentControl = new ContentControl();
                    contentControl.Content = channelguide.TheSelectedScheduleItem;

                    contentControl.ContentTemplate = SelectedScheduleItem;

       private DataTemplate contentcontrolitem;
                public DataTemplate SelectedScheduleItem
                {
                    get { return contentcontrolitem; }
                    set
                    {
                        contentcontrolitem = TheChannelScheduleItem();
                    }
                }


               private DataTemplate TheChannelScheduleItem()
               {
                   Grid grid = new Grid();

                   StackPanel stackpanel = new StackPanel();
                   stackpanel.Orientation = Orientation.Horizontal;
                    StackPanel stackpanel1 = new StackPanel();
                   stackpanel1.Orientation = Orientation.Vertical;

                       grid.Children.Add(stackpanel);

                          return grid;

    }

    Can anyone help me ?     

              

    Thursday, March 21, 2013 5:28 PM

Answers

  • Hi,

    Sorry for the late response.. I was not able to do it as suggested above.

    But I set the Content for each cell in the Canvas to a Property in the CanvasTapped Event and also binded the XAML Control template to that Property and it gets bound and display the action I require.

    Thanks,
    Alekhya.

    • Marked as answer by Ale_dev Tuesday, April 16, 2013 6:44 PM
    Tuesday, April 16, 2013 6:44 PM

All replies

  • There is a solution on StackOverflow that might work for you.

    Create data template in win8.

    Thursday, March 21, 2013 5:42 PM
  • Hi,
    Thanks for the solution.. I tried implementing it..but it wasn't binding all the properties for controls.

    var str = "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" +
                                  "<Grid>"+
                                 "<StackPanel Orientation=\"Horizontal\">"+
                     "<StackPanel Orientation=\"Vertical\">" +
                         "<TextBlock  Text=\"{Binding ProgramTitle}\" Style=\"{StaticResource TheChannelScheduleItemTextBlockStyle}\" />" +
                        "<StackPanel Orientation=\"Horizontal\"  Visibility=\"{Binding IsFooter, Converter={StaticResource BoolToCollapsedConverter}}\">" +
                            "<TextBlock Padding=\"0,5,0,5\" Text=\"{Binding Path=ProgramAirTime}\" Foreground=\"#FF333333\" FontSize=\"14\" FontWeight=\"SemiBold\"/>" +
    
                            "<TextBlock Padding=\"5,5,5,5\" Text=\"|\" Foreground=\"#FF333333\" FontSize=\"14\"  FontWeight=\"SemiBold\"/>" +
    
                            "<TextBlock Padding=\"0,5,0,5\" Text=\"{Binding Path=Duration}\" Foreground=\"#FF333333\" FontSize=\"14\"  FontWeight=\"SemiBold\"/>" +
                            "<TextBlock Padding=\"0,5,0,5\" Text=\" min  \" Foreground=\"#FF333333\" FontSize=\"14\"  FontWeight=\"SemiBold\"/>" +
    "</StackPanel>" +
                "</StackPanel>" +
            "</Grid>" +
        "</DataTemplate>";
    
        DataTemplate template = (DataTemplate)Windows.UI.Xaml.Markup.XamlReader.Load(str);
    
                    contentControl.ContentTemplate = template;
    As I attached the code above, my textblock text property doesn't get binded to the VM?

    Thursday, March 21, 2013 6:19 PM
  • Hi,

    I cannot reproduce your issue, I used these codes and it works fine.

                var str = "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" +
                       "<StackPanel Orientation=\"Vertical\">" +
    
                "<TextBlock Text=\"{Binding Path=Foo}\" FontSize=\"14\"  FontWeight=\"SemiBold\"/>" +
                "<TextBlock Text=\"{Binding Path=Bar}\" FontSize=\"14\"  FontWeight=\"SemiBold\"/>" +
    "</StackPanel>" +
    "</DataTemplate>";
                DataTemplate template = (DataTemplate)Windows.UI.Xaml.Markup.XamlReader.Load(str);
                ContentControl contentControl = new ContentControl();
                contentControl.Content = new MyItem { Foo = "Hello", Bar = "World" };
                contentControl.ContentTemplate = template;
                this.Content = contentControl;

    Would you please upload a sample codes to reproduce this issue?

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, March 25, 2013 8:52 AM
    Moderator
  • Hi Jesse, 
    Sure, I can paste the sample code for clear idea.. my problem with this procedure is- my textblock properties are bound to something else from another ViewModel, and it doesn't get the value from that VM.

     public class CanvasEx : Canvas
        {
    static int ROW_HEIGHT = 63;
          
     public CanvasEx()
                : base()
            {
                Debug.WriteLine("CanvasEx");
    
                Loaded += CanvasEx_Loaded;
             }
    
    
    private void canvas_Tapped(object sender, TappedRoutedEventArgs e)
     { ContentControl contentcontrol = new ContentControl();
     contentcontrol.Content = channelguide.TheSelectedScheduleItem;
     contentcontrol.ContentTemplate = TheChannelScheduleItem();
    }
    
    
                private DataTemplate ChannelScheduleItem()
                {
                  
                    var str = "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" +
                    "<Grid DataContext=\"TheSelectedScheduleItem\">" +
                       "<StackPanel Orientation=\"Horizontal\">" +
                    "<StackPanel Orientation=\"Vertical\">" +
                   "<TextBlock  Text=\"{Binding ProgramTitle}\"/>" +
                   "<StackPanel Orientation=\"Horizontal\">" +
                          "<TextBlock Padding=\"0,5,0,5\" Text=\"{Binding Path=ProgramAirTime}\" Foreground=\"#FF333333\" FontSize=\"14\" FontWeight=\"SemiBold\"/>" +
                           +
                                            "<Image  Margin=\"0,5,0,5\"  Source=\"{Binding Path=FormatImage, Converter={StaticResource ImageNameToImageSource}}\"/>" +
    
                                        "</StackPanel>" +
                         "</StackPanel>" +
                 "</Grid>" +
               "</DataTemplate>";
    
                    DataTemplate template = (DataTemplate)Windows.UI.Xaml.Markup.XamlReader.Load(str);
    
                    return template;
    
                }
    
    }
    

    so in my textblock..the textblock's text property is bound to ProgramAirTime which is defined in another ViewModel -> so it doesn't get bounded and it does not display anything.

    In my canvas tapped event.. im trying to create a popup which will have the contentcontrol which all this information. Am i doing it right? 

    Monday, March 25, 2013 7:15 PM
  • Hi,

    Would you please upload a sample codes to skydriver?

    What's the control name of channelguide in your project, we cannot reproduce your issue with these codes.

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 26, 2013 3:15 AM
    Moderator
  • Hi,

    Sorry for the late response.. I was not able to do it as suggested above.

    But I set the Content for each cell in the Canvas to a Property in the CanvasTapped Event and also binded the XAML Control template to that Property and it gets bound and display the action I require.

    Thanks,
    Alekhya.

    • Marked as answer by Ale_dev Tuesday, April 16, 2013 6:44 PM
    Tuesday, April 16, 2013 6:44 PM