Draw solid color triangle using XAML only
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
You can draw a solid-color triangle in pure XAML without any code-behind by using a shape element and defining three points. In WPF, the simplest tool for this is Polygon, which fills the area enclosed by the points you provide.
Use Polygon for the Straightforward Case
The simplest triangle is:
That produces a filled triangle with no outline. The Points attribute lists the vertices in order as x,y coordinate pairs.
Because there are exactly three points, the polygon is a triangle. The Fill brush paints the interior.
Add an Outline if Needed
If you want a border around the triangle, add Stroke and StrokeThickness:
This is still XAML-only. No code is required unless you want to change the shape dynamically at runtime.
Understand the Coordinate System
WPF uses a top-left origin for layout coordinates:
- larger
xmoves right - larger
ymoves down
So in the earlier example:
- '
60,0is the top vertex' - '
0,100is the bottom-left vertex' - '
120,100is the bottom-right vertex'
Changing those coordinates changes the triangle shape immediately. For example, a right-pointing triangle could be written like this:
That is useful for arrow icons, expand/collapse markers, and directional indicators.
Path Is an Alternative
Polygon is usually easier for simple triangles, but you can also use a Path:
This does the same job with path geometry commands:
- '
Mmeans move to the first point' - '
Lmeans draw a line' - '
Zcloses the figure'
Path becomes more useful when the shape is part of a larger vector drawing. For a simple triangle, Polygon is usually easier to read.
Keep Sizing and Stretching in Mind
A common surprise is that hard-coded points define the triangle in a specific coordinate space. If the container changes size, the triangle may not scale the way you expect unless you design for that.
For many fixed icons and decorations, hard-coded points are perfectly fine. If you need the shape to scale fluidly with the layout, a Viewbox can help:
This preserves the triangle proportions while letting the rendered size change.
Common Pitfalls
The biggest mistake is providing points in the wrong order or with the wrong coordinates and then assuming the fill is broken. The triangle is only as correct as the points you define.
Another common issue is forgetting that WPF coordinates grow downward on the y axis. If you think in traditional math coordinates, the triangle may appear upside down from what you expected.
It is also easy to reach for code-behind when XAML alone is enough. For a static solid triangle, Polygon with a Fill brush is all you need.
Summary
- In WPF XAML,
Polygonis the simplest way to draw a solid-color triangle. - A triangle is just a polygon with three points.
- Use
Fillfor the solid interior andStrokeif you want an outline. - '
Pathis a valid alternative when the triangle is part of a more complex vector shape.' - Pay attention to coordinates and sizing so the triangle appears in the intended direction and scale.
Related reading
- Duplicate keys in .NET dictionaries?
- dynamic does not contain a definition for a property from a project reference
- Easier way to populate a list with integers in .NET
- Easiest way to compare arrays in C
- EF Core add-migration Build Failed
- EF LINQ include multiple and nested entities
- Efficient use of reflection in C
- Empty toolbox in Visual Studio 2022 for .NET 6.0 WinForms and Control Library projects

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.