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

  • You may already know about some of the benefits of Bluemix, IBM's open platform for developing and deploying mobile and web applications. Check out this webcast that focuses on building an Android application using the MobileData service, with a walk-through of the real process and workflow used to build and link the MobileData service within your application. Join IBM's subject matter experts as they show you the way to build a base application that will jumpstart you into building your own more complex app …

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds