How to add comments into a Xaml file in WPF?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
WPF XAML files use XML syntax, so comments follow the XML comment rules rather than a C# style line comment. The mechanism is simple, but the important detail is knowing where comments are valid and where they will break parsing.
Use XML Comment Syntax
In XAML, comments use the standard XML form <!-- comment -->. You can place them between elements, above controls, or around sections you want to temporarily disable while editing.
That is the normal, supported way to document structure in a XAML file.
Comment Out Existing Markup
Because comments can wrap whole XML fragments, they are useful when you want to temporarily remove a block from the visual tree during debugging.
This works well for short experiments, especially while testing layout changes. It is still better to delete dead markup once you know the block is no longer needed.
Where Comments Are Not Allowed
XML comments are not valid everywhere. The two rules that matter most are:
- do not place a comment inside an attribute value
- do not nest one XML comment inside another
For example, this is invalid because the comment appears inside an attribute value:
And this is invalid because XML comments cannot be nested:
If you need to annotate an attribute choice, put the comment on the line above the element instead.
Keep Comments Focused on Intent
The best comments explain why a block exists, not what the markup obviously says. A comment such as “button control” adds no information. A comment such as “kept in the tab order for keyboard accessibility” is useful because it explains intent.
This matters in XAML because the markup is already descriptive. Over-commenting makes the file harder to scan and encourages stale notes that no longer match the UI.
A practical guideline is to comment these cases:
- unusual layout workarounds
- interoperability with code-behind or custom controls
- accessibility or localization constraints
- temporary diagnostics that will be removed soon
Comments and the WPF Designer
The Visual Studio XAML designer generally ignores comments, which is what you want. They are for maintainers, not for runtime behavior. If a comment seems to affect parsing, the real issue is usually malformed XML around it rather than the comment itself.
When a XAML file stops loading in the designer after a comment edit, check for these problems first:
- an unclosed comment marker
- an illegal nested comment
- accidental removal of an element boundary near the comment
Those mistakes are easy to make when commenting out a large block.
A Small Realistic Example
The following example shows useful comments without cluttering the file.
The comments tell the next developer something the markup alone does not.
Common Pitfalls
A common mistake is using comments to hide large amounts of obsolete XAML for long periods. That turns the file into an archive instead of a source file. Remove code that is not coming back.
Another mistake is writing comments that restate the visible control names. If the comment is obvious, it should probably be deleted.
Developers also run into parser errors by forgetting that XML comments cannot be nested. When disabling a large section, inspect the block first for existing comments.
Finally, do not use comments to store TODO lists that belong in issue tracking. A short local note is fine, but long-lived work items age badly inside markup files.
Summary
- XAML comments use standard XML syntax:
<!-- comment -->. - Place comments between elements or around blocks, not inside attribute values.
- Do not nest XML comments.
- Use comments to explain intent, constraints, or temporary debugging context.
- Delete obsolete commented-out markup instead of keeping it indefinitely.

