Skip to main content

Posts

Showing posts from September, 2013

C# WPF Tutorial - Priority Bindings [Beginner]

Bindings, bindings, bindings. WPF is all about bindings - and you can't be all about bindings unless there are a whole bunch of different types to play with. We have looked at a couple different types here - I won't list them all, it is probably just easier to look at list in the WPF category . Today we are going to introduce yet another type of binding - the Priority Binding. What is a priority binding, you ask? Well, it is a binding with a number of possible sources, with the sources ordered by priority. The source with the highest priority that returns a value successfully becomes the active binding in a priority binding. The key here, though, is that the active binding can change. If a low priority source returns first, it will become the active binding. But as soon as a source with higher priority returns successfully, it will supersede the lower priority source and become the active binding. You are probably wondering about how this is actually useful. Well, ...

C# WPF Tutorial - Using Splash Screens [Beginner]

Splash screens - everyone loves them, right? Actually, I'm not a big fan of them, but sometimes they are a necessary evil - especially in the world of WPF. A cold start up of a WPF application can be, well, slow - mostly because there are a lot of common WPF dlls that need to be loaded. Once they are loaded for the first time after boot up, WPF apps generally start pretty fast, but that first WPF app after a reboot can crunch away at your hard drive for quite a few seconds. Adding a splash screen for WPF in the .NET 3.5 SP1 world was a huge pain. You had to pull in a couple native methods and essentially create your own window, and on top of all that there was a special build step that had to be executed. But now, it's so easy, it's almost not worth a tutorial (but I'm going to write it up anyway, cause hey, you never know). Adding a splash screen for a WPF app has been turned into a three step process now that SP1 is out . The first step is to make the spl...

C# WPF Snippet Tutorial - Aligning ListView Items [Beginner]

WPF is powerful. So powerful in fact, that sometimes it's hard to find styles and settings to make it do what you want. Aligning ListView items was one such example for me. This snippet tutorial will show you how to use a style to vertically and horizontally align the contents of ListView cells. I came across the need to vertically align the contents of my ListView when I had columns that contained elements that were different heights. By default, the ListView vertically aligns all of the content to the middle. I wanted them aligned to the top. Here's a quick application I threw together to demonstrate what I'm talking about. It has a ListView bound to a collection of VideoGame objects. Each video game contains the title and an image of the box art. First, the object and some code to populate it: public class VideoGame { public string Name { get ; set ; } public string Image { get ; set ; } } ... public p...

C# - How To Use A Datatemplate Selector In WPF [Beginner]

DataTemplates are an extremely powerful part of WPF, and by using them, you can abstract all sorts of display code. However, there are times when they fall short - and initially when I was learning WPF I was disappointed by that. For instance, you only get to set one DataTemplate on an items control, and while that made sense, it felt limiting. What if I wanted to use different templates depending on the content of the item? Do I have to build all that logic into a single data template? And then I discovered DataTemplateSelectors! They are WPF's answer to the question I posed above - they let you write some logic that chooses what data template to use for an item. You could even, say, create an entirely new data template on the fly if you needed to. And it helps with one of WPF's main goals, separating out display (the DataTemplate itself) from logic (the DataTemplateSelector). We are going to write a simple little application today, that is essentially just a list...

C# - Binding Converters In WPF [Beginner]

I'm going to come right out and say it, binding converters are one of the nicest pieces of WPF I've run across so far. Simply put, they provide a translation between your binding source and destination. The most common use I've found is when I'm binding to user interface elements, because on many occasions I don't store something in a data member that can just be stuck on the screen. This tutorial will provide an introduction to what binding converters are and how to use them. The example application I'm going to make today is going to be a traffic light. The graphics will all be written in XAML and will be bound to an object that represents the state of the light (green, yellow, and red). Let's start off with the object. This is a very simple object and should be very straight forward. public class TrafficLight : INotifyPropertyChanged { /// <summary> /// Fired whenever a property changes. Required for /// INotifyProperty...