C# – WPF – 23 Well Written WPF Control Examples

In this article I present a code from the book “Pro WPF 4.5. in C#“, which I consider useful and interesting. This example has impressed me with its structure – it simply gives good practises for creation of 23 different controls within XAML. Once you run the example, you get the following window:

Untitled23

You may click on  any button, in order to check the control and its execution. These are the options:
1. Embedded Font
2. Gdi Text Rendering
3. Buttons With Content
4. Button With Shape
5. Label Test
6. Radio Button Groups
7. Tooltips
8. Popup Text
9. Scrollable Text Box Column
10. Logical Scrolling
11. Graphical Tab Titles
12. Expandable Content
13. MultiDirectional Expanders
14. Expandable Scrollable Content
15. Text Box Test
16. Custom Spell Checker
17. Image List
18. Check Box List
19. Combo Box Test
20. Slider Test
21. Sliders Compared
22. Indeterminate Progress Bar
23. Date Controls


 1. Embedded Font

Simply we use a font, which is available in the folder of the application. In order to embed the fount we use the command “./#Bayern”. The, the name of the file, where the font is present is “bayern.ttf”. The code in XAML looks like this:

 2. Gdi Text Rendering

Back to the list
This example simply shows us how the XAML what is it when we have ” TextOptions.TextFormattingMode=”Display” ” and when we do not have it. The XAML code looks like this:

 3. Buttons With Content

Back to the list
This example presents simply the creation of 4 buttons with content in a stack panel. This is the XAML code:

 4. Button with shape

Back to the list
This is more interesting – you receive a shape in the button 🙂 How do you get it? Quite easy actually, just with adding a grid to the button and defining polygon points in it:

 5. Label Text

Back to the list
The idea of this example is to show how to enable the selection of a specific object, using the ALT key and a predefined character. The magic is done in the naming, where you should use the underscore sign “_” before the character you would like to enable. It looks like this:

 6. Radio Button Groups

Back to the list
In this example, the trick is to name the groups correctly. Although, the groupboxes are the same within the radio buttons, what matters for the selection is the “GroupName” within the StackPanel:

  7. Tooltips

Back to the list
With this example we receive four different types of tooltips – a standard one, a tooltip with picture within, a tooltip which is formatted to go to the left and a tooltip associated to the StackPanel and not to a specific button. Thus we see that if we have two objects with different tooltips (in our case a stack panel and a button within the stack panel), the displayed tooltip is the more detailed one:

8. PopUp Text

Back to the list
The PopUp Text is simply the solution if you would like to have a clickable tooltip, containing button or a link. The following example contains link to wikipedia:

 9. Scrollable TextBox Column

Back to the list
With this example we get a TextBox, which is scrollable. It is made with the creation of a grid, which is bigger than the window itself. Thus we need to scroll. The scrolling itself is achieved through assigning four buttons to void methods in C#. This is the C# code:

This is the XAML code, for the creation of the application:

 10. Logical Scrolling

Back to the list
Here the idea is, that if you click on a button, which is half seen at the window, the scrolling works automatically and makes it completely visible. As you can imagine this feature should not be difficult for achieving, thus it is just a boolean property of “Scroll Viewer” – “CanContentScroll”:

 11. Graphical Tab Titles

Back to the list
The TabItem in XAML is a content element. As such it may contain anything a content element wants 🙂 In this example, our TabItem contains a picture:

 12. Expandable Content

Back to the list
Simply an expander in a stack panel. Three expanders are present in the example. The first one contains a button, the second one text. The third one is left expanded:

 13. Multidirectional Expanders

Back to the list
Here we have quite the same example as before, with the difference that there is a property of the expander, named “ExpandDirection”. It can be “Up”, “Left” or “Right”. When it is not set, it is down:

 14. ExpandableScrollable Content

Back to the list
We simply have a scroll viewer in the expander. Cool:

 15. TextBoxTest

Back to the list
Here we have something interesting! (with some C# code behind) – we have TextBox and a TextBlock. Once, something in the TextBox is selected, it is displayed in the TextBlock. Nice:

How do we achieve it? With creation a C# void method “private void txt_SelectionChanged”. Four lines in the method do the magic:

16. Custom Spell Checker

Back to the list
The custom spell checker is an option for a spell check, based on a predefined list of words. In the example, the list of words is a simple text file with a “*.lex” extension. In the example we have a text box in which some text is entered. Then through “<SpellCheck.CustomDictionaries><sys:Uri>” we give the path to our *.lex file, containing the words, which are included in the spell check. The code looks like this:

 17. Image List

Back to the list
Here we have a List Box, containing 3 stack panels. In each stack panel we have image and label. Once a selection is changed, the method “lst_SelectionChanged” is activated. In order to ease the example, the method is empty. It is simple and it looks OK:

 18. CheckBox List

Back to the list
Here we have a a listbox and a stack panel in a grid. What does this application do? Simply displays information about the performed selections in a text block. Two methods help it to do this. The method “lst_SelectionChanged”, which is activated upon a change of the listboxes and a “cmd_CheckAllItems”, which is activated by a button:

The XAML file is a neat one:

19. Combo Box Text

Back to the list
Do you think you can put a picture on a drop-down menu? Yes? Well, in this case you are probably right. The book “Pro WPF 4.5. in C#” gives you a good way to do it:

 20. Slider Test

Back to the list
Sometimes we need a slider in WPF. XAML gives us a good opportunity to create one, using the “<Slider>”. It is easy and understandable. In the below example, we set Margin, TickFrequency, TickPlacement, Maximum, Delay and Interval properties:

 21. Sliders Compared

Back to the list
Here the author gives us four slider examples. Normal slider, slider with tick marks (these are the marks below the line of the slider), slider with irregular tick marks and slider with selection range. The slider with selection range is a slider, where you simply define the range, which should be colored in blue (the default color). This is how it looks like:

 22. Interdeterminate Progress Bar

Back to the list
Just a progress bar, which keeps on rolling:

 23. Date Controls

Back to the list
Date controls – having <DatePicker> and <Calendar> in XAML makes really a good job for us. With a few lines we get beautiful result:

However, in order to make it more beautiful, the author of the book has decided to put some C# coding in the example. Thus, it is not allowed to pick a date from the weekends and if you select some imaginary date from the date pickers on the right you get an error as well. The errors are displayed in a text block, which is named “lblError”. Here is the C# code with the two methods, helping us to achieve this:


To summarize, chapter 6 of the book is really a must-read chapter for everyone dealing with C# and XAML. It simply gives you good practices for the creation of controls.

Tagged with: , , , , , , , , , , , , , , , , , , , , , , , , , , ,