#region in XAML

WpfVisual StudioXaml

Wpf Problem Overview


I actually don't like #region in my code. BUT for some reason call me crazy, I would like to have them in my XAML. I would like whole sections to have a #region-like thing and collapse them (e.g. my <Window.CommandBindings>, <Grid.*Definitions>, <Menu>, <Toolbar>, etc..

Does this exist? If not, how about <RegionCollapse>

Wpf Solutions


Solution 1 - Wpf

OP NOTE: this was the original right answer and in some ways still is a good and for some best answer including me. BUT this answers the original question best I think.

Visual Studio 2015 finally adds support for regions in XAML. You enclose your regional code like this:

<!--#region RegionName-->

All Your XAML In Here

<!--#endregion-->

This will add a collapse icon to the left of starting line, using which you can expand/collapse the region.

Don't know how handy this would practically be since we can already collapse/expand any node in XAML editor. It's there anyway if you want to use.

Solution 2 - Wpf

Solution 3 - Wpf

I might be missing the point of your request, but the XAML editor automatically puts collapsible regions around the elements. Note the nodes in the left rail of the editor window. Click the "-" and the associated element collapses to a single line, much like a #region.

Solution 4 - Wpf

No, but according to this reply by Microsoft:

> This is a great suggestion. Thanks very much. We will consider it for a future release

Solution 5 - Wpf

FYI: This one has been updated to work well with VS 2012 http://visualstudiogallery.msdn.microsoft.com/3c534623-bb05-417f-afc0-c9e26bf0e177

And this VS2012 extension does a nice job of beautifying the XAML so attributes automatically sort and align etc. http://xamlstyler.codeplex.com/documentation

Solution 6 - Wpf

In Visual Studio 2012 Update 4, I was able to select a very large section of XAML code, right click and choose "hide selection". (basically collapses that section with the [...] notation, and after expanding it, the system "remembers" that I wanted to collapse that portion with the [-] notation next to the line numbers.

Solution 7 - Wpf

You can add comments to the XAML file using SSI formatting:

<!-- Grid Styles -->
<style...>
<style...>

<!-- Window Styles -->
<style...>
<style...>
<style...>

This at least gives you some sort of "header" if you use the built-in XML collapse.

Solution 8 - Wpf

Yes,

VS2010 will remember the collapsed areas but beware this stopped working when I applied SP1 (as did a lot of other things). After uninstalling VS2010 (and any extensions) and doing the following install procedure the XAML elements that were collapsed seemed to be remembered fine:

  • Install VS2010 (Pro in my case)
  • Install VS2010 windows updates (including an XML editor one, 200MB+)
  • Install SP1 Install
  • Install VS2010 SP1 windows updates

Might help someone.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkennyView Question on Stackoverflow
Solution 1 - WpfdotNETView Answer on Stackoverflow
Solution 2 - WpfJacobJView Answer on Stackoverflow
Solution 3 - WpfHans PassantView Answer on Stackoverflow
Solution 4 - WpfJustin EthierView Answer on Stackoverflow
Solution 5 - WpfClint StLaurentView Answer on Stackoverflow
Solution 6 - WpfrazblackView Answer on Stackoverflow
Solution 7 - WpfLance ClevelandView Answer on Stackoverflow
Solution 8 - WpfScaramangaView Answer on Stackoverflow