Seven WPF Programming Tips and Tricks

The internet has so much to offer on any topic and there's so much to learn; but where to start? I realized many years ago, when I first began to be intrigued by the Windows Presentation Foundation (WPF), that short write-ups help you gain incremental knowledge, which when combined can offer solutions to the bigger picture. This article offers a list of tips that I learned around a year ago, while scavenging for simple solutions to big problems.

  • Use the Visibility.Collapsed Enumeration vs. Visibility.Hidden

    The Collapsed value ensures that the element does not participate in the layout and gives it a height and width of zero. The latter causes the element to continue to participate in layout.

  • Reduce CPU Consumption for WPF Animations

    As you know, WPF draws animations at 60 frames per second. You can reduce this to a lower optimal rate, resulting in less CPU usage. Use Timeline.DesiredFrameRateProperty to change the default value; set it to a lower value like 15, and then change it according to the smoothness that you desire.

    Timeline.DesiredFrameRateProperty.OverrideMetadata(
                    typeof(Timeline),
                    new FrameworkPropertyMetadata { DefaultValue = 15 }
    );
    
  • Default the Culture of the Application to the Culture of Your Client's Machine

    WPF elements expose a language property that can be used to assign languages as per any culture. When this property is applied to a Window, it applies to all the elements that it contains. Look at the sample code that applies the language to the current culture. The Startup function in the App class is an ideal place to apply this change across the application.

    protected override void OnStartup(StartupEventArgs e)
    {
    FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(System.Globalization.CultureInfo.CurrentCulture.IetfLanguageTag)));
    
    	base.OnStartup(e);
    }
    
  • Use StaticResource vs. DynamicResources StaticResources are evaluated only once and only during the object. Dynamic resources cause evaluation each time they are requested by the control, and hence make the application perform slower. Although this is a deferred runtime lookup, it makes sense if the resource is not going to change

  • Use WPF Performance Suite to Profile Your WPF Applications

    WPF Performance Suite is available in the Windows SDK and consists of few important profiling tools that allow you to analyze the applications and provide information on the optimizations that can be applied. You can download it from the Microsoft's web site.

    The Perforator and Visual Profiler profiling tools give you a lot of information and also indicate potential bottlenecks in the application. This suite is part of the Microsoft Windows SDK for Windows 7 and .NET Framework 3.5 SP1.

  • Avoid Designer Errors with the Help of DesignerProperties.GetIsInDesignMode Method

    Most often designers break with the "Could not create an instance" error. Although this is due to some unhandled exception occurring in the constructor of the control, it may not require running the code in the design time.

    public SampleControlConstructor()
    {
        InitializeComponent();
        if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
        {
            //place your code here.
        }
    }
    
  • Use IsMouseDirectlyOver Event to Determine the Exact Mouse Movement.

    The IsMouseOver event would respond to the mouse movement within a control or its children.

    Use the IsMouseDirectlyOver event to determine if the mouse movement is on the control and not its containers. This is useful to build specific triggers that change elements on mouse movement.

I hope these help you save some precious programming time. I will be back very soon with some more tricks. Enjoy!



About the Author

Srinath M S

I would love to leave a footprint in this flat world

Related Articles

Comments

  • Great tips!

    Posted by Vcoder on 08/20/2014 05:17pm

    Thank you for your effort!

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Agile methodologies give development and test teams the ability to build software at a faster rate than ever before. Combining DevOps with hybrid cloud architectures give teams not just the principles, but also the technology necessary to achieve their goals. By combining hybrid cloud and DevOps: IT departments maintain control, visibility, and security Dev/test teams remain agile and collaborative Organizational barriers are broken down Innovation and automation can thrive Download this white paper to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds