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.

                    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)));
  • 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()
        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


  • 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

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

  • Event Date: April 15, 2014 The ability to effectively set sales goals, assign quotas and territories, bring new people on board and quickly make adjustments to the sales force is often crucial to success--and to the field experience! But for sales operations leaders, managing the administrative processes, systems, data and various departments to get it all right can often be difficult, inefficient and manually intensive. Register for this webinar and learn how you can: Align sales goals, quotas and …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds