2

Lets say I want to make the following a DataTemplate:

StackPanel stackPanel = new StackPanel();

Button button1 = new Button();
button1.Click += button1_Click;
Button button2 = new Button();
button2.Click += button2_Click;

stackPanel.Children.Add(button1);
stackPanel.Children.Add(button2);

How can I turn this into DataTemplate? I tried creating an instance of DataTemplate class, but I see no method or property that would let me set the stackpanel above as the content of the DataTemplate object.

EDIT: I am trying to do this in a Windows Store app.

  • I think you could try assigning the `StackPanel` to the `DataTemplate` object's `VisualTree` property, but that is just a guess. Why don't you just build the template in XAML? – Tony Vitabile Jul 08 '14 at 14:30
  • DataTemplate object doesn't have VisualTree property, unfortunately. And I can't do it in XAML for reasons that are too complex to explain right now :/ @TonyVitabile – Ognjen Koprivica Jul 08 '14 at 14:36
  • Actually, according to this: http://msdn.microsoft.com/en-us/library/system.windows.datatemplate(v=vs.110).aspx, it does. It's inherited from `FrameworkTemplate`. – Tony Vitabile Jul 08 '14 at 15:21
  • Yeah, but I am building a Windows Store app. The only availaible class named "DataTemplate" would be this: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.datatemplate.aspx @TonyVitabile – Ognjen Koprivica Jul 09 '14 at 08:12

2 Answers2

4

The accepted answer to this question shows how you build a DataTemplate in the code behind.

//create the data template
DataTemplate objDataTemplate = new DataTemplate();
objDataTemplate.DataType = typeof(CreditCardPayment);

//set up the stack panel
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(StackPanel));
spFactory.Name = "myFactory";
spFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal); // Use Oritentation.Vertical if appropriate.

//Your code for creating the buttons goes here.

//set the visual tree of the data template
objDataTemplate.VisualTree = spFactory;

That should do it.

Community
  • 1
  • 1
Tony Vitabile
  • 8,298
  • 15
  • 67
  • 123
  • Unfortunately, Windows Store apps don't have access to the mentioned FrameworkElementFactory and DataTemplate classes :/ This is the only availaible class named DataTemplate: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.datatemplate.aspx – Ognjen Koprivica Jul 09 '14 at 08:19
  • Sorry, your question originally didn't say that. Why don't you try building a test app with a simple `DataTemplate` in XAML in the window's resources, then get a reference to the resource & inspect it? That will show you what you have to build, then you can back into what code to write to produce the similar template. – Tony Vitabile Jul 09 '14 at 12:29
  • That makes a lot of sense, will try that. Thank you! @TonyVitabile – Ognjen Koprivica Jul 10 '14 at 09:31
0

Create a new StackPanel, add the DataTemplate object as a child of it.

StackPanel stackPanel = new StackPanel();
stackPanel.Children.Add(objDataTemplate);

Are you looking for this (although you will not able do it like this), or I misunderstood your question?

UPDATE: For the opposite

DataTemplate objDataTemplate = new DataTemplate(typeof(StackPanel));
        FrameworkElementFactory stPanelElement = new FrameworkElementFactory(typeof(StackPanel));
        objDataTemplate.VisualTree = stPanelElement;

Hope this will help.

Debasis
  • 408
  • 1
  • 3
  • 12