WPF - Custom Window Styles

This article and the sample project attached shows techniques to create a custom WPF window style. It was developed on the .Net 3.5 Framework with VS 2005.

This demonstrates a simple window with a custom style having rounded borders and a few sample controls. The form does not have a header section in the UI element during display at runtime but can be moved around nonetheless by clicking the body and dragging the mouse to the desired position. A close button completes the basic form handling. The controls embedded in the XAML really do nothing other than provide a look and feel of a form meant to handle some tasks!

The image below shows a runtime view of the custom window. (Pardon my color selections if they assault the senses!)



The Window styles are created using the Path class. The Path class provides the ability to draw curves and complex shapes. These curves and shapes are concretely described using Geometry objects. In a Path, you create a Geometry and use it to set its PathGeometry member.

<!--Non-Rectangular window edge, created with PathGeometry-->
<Path Stroke="DarkGray" StrokeThickness="2" Height="346" Width="1032" Opacity="0.9">

PathGeometry in turn has the PathFigure member. Here you set the start point of the shape. Specifically, the geometrical layout \ design is accomplished the LineSegment and ArcSegment classes. These set the Path object's PathFigure property.

<Path.Data>
    <PathGeometry>
        <PathFigure StartPoint="40,20" IsClosed="True">
            <LineSegment Point="990,20" ></LineSegment>
              <ArcSegment Point="1020,50" Size="40,55" SweepDirection="Clockwise">
              </ArcSegment>
        </PathFigure>
    </PathGeometry>
</Path.Data>

The Path.Fill member describes how the interior of the shape is painted. Here it is possible to specify the colors and the gradients of those colors. The color fill and gradients are specified using LinearGradientBrush and GradientStop members inside of the Path.Fill member.

<Path.Fill>
    <LinearGradientBrush StartPoint=".2,0" EndPoint="0.8,1">
        <GradientStop Color="Lightgray"  Offset="0">
        </GradientStop>
    </LinearGradientBrush>
</Path.Fill>

All this is achieved using the Canvas UI element as the base member of the WPF form. The Canvas panel is for creating complex drawings because it supports absolute positioning of all of its child objects. Canvas elements can also be used to specify transparency property using the Opacity, something that is also seen in this sample.



About the Author

Renji Daniel

.Net devloper with 10+ years of design and development experience

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Hurricane Sandy was one of the most destructive natural disasters that the United States has ever experienced. Read this success story to learn how Datto protected its partners and their customers with proactive business continuity planning, heroic employee efforts, and the right mix of technology and support. With storm surges over 12 feet, winds that exceeded 90 mph, and a diameter spanning more than 900 miles, Sandy resulted in power outages to approximately 7.5 million people, and caused an estimated $50 …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds