Design View of a DataTemplate in a ResourceDictionary in Visual Studio
Asked Answered
M

2

16

Is there a way to preview GUI in a DataTemplate? e.g. if I have a Resource Dictionary, containing a DataTemplate, and the Data template contains GUI code, like

<DataTemplate x:Key="MyTemplate">
    <StackPanel>
        <TextBox Text="AAA"/>
    </StackPanel>
</DataTemplate>

Can I view just the stackpanel? (like previewing a UserControl in design view). If not possible with original VS, is there a plugin for that?

Marriage answered 10/4, 2013 at 9:52 Comment(4)
Expression Blend can do it. But i´m not aware of any tool for visual studio that visualizes the DataTemplatePreciosa
Unless you want to copy the StackPanel into a blank UserControl and use the VS design view editor, edit and the copy back to the DataTemplate, you should use Expression Blend as @Preciosa said. I did this myself in some occations when I didn't feel like using Blend.Birmingham
That would have been a nice feature for Visual Studio 2013, for people like me that don't like to use Expression Blend. Another way would be to create a separate XAML file containing the DataTemplate (you don't have to create a UserControl) and then link your XAML file in your ResourceDictionary.Sphygmoid
@TheodoreZographos how to do this in particular. This is exactly what I am looking forMaypole
R
20

Visual Studio doesn't support doing this, but Expression Blend does, if you can get access to it. Here's some instructions on several ways to edit template resources:

http://msdn.microsoft.com/en-us/library/cc294908(v=expression.40).aspx

In particular, if you'd like to edit a DataTemplate located in a ResourceDictionary you would:

  • Load the project in Blend
  • Locate the .xaml file containing the ResourceDictionary in the Resources panel (docked right by default)
  • Locate the DataTemplate in the list of resources (by Key)
  • Click Edit Resource

I know this an old question but I hope this helps!

Risa answered 15/2, 2014 at 3:20 Comment(1)
This doesn't work for projects targeting Windows 10 Fall Creators Update, and possibly earlier versionsContributor
P
1

If you reference your template down in the body of your control, such as ItemTemplate="{StaticResource MyTemplate}", you can place your cursor on the name of the template and press F12 to have Visual Studio render the template in the XAML designer.

For some reason, this only works once. You have to close and reopen the form to see another template rendered. Surely there's some way to avoid this but I haven't been able to figure it out.

Preconscious answered 8/12, 2016 at 20:17 Comment(1)
Not for VS2022 in UWP; F12 on a {StaticResource MyResourceName} just moves the cursor to the definition of the resource in the XAML. No visual render.Bargello

© 2022 - 2024 — McMap. All rights reserved.