Friday, 6 June 2014

WPF Elements and Controls: Basic Concepts and Questions

WPF Elements and Controls: Basic Concepts and Questions

In this article, I have covered very basic concepts of WPF Elements and Controls in the form of questions and answers like the difference between elements, controls, content controls, headered content controls and layout containers, difference between tooltip and popup, difference between radiobutton and checkboxes, lexicon file etc. Last question illustrates the difference between resource files and content files in WPF (this question is out of the blog post title as these files are not WPF elements).

1. What is the difference between WPF Elements and WPF Controls?

There is a difference between WPF Elements and WPF Controls. All the WPF Controls are WPF Elements but vice-versa is not true. Any WPF element which can receive focus and accepts inputs from keyboard or mouse is called WPF Control.

For example: Textbox, Button are WPF Controls.

Exceptions: Tooltip and Label are also considered as WPF Controls because Tooltip appears and disappears depending on user's mouse movement. Similarly, Labels support mnemonics (shortcut keys), that's why Labels are also considered as WPF Controls.

Note: All the WPF Controls are derived from System.Windows.Control class.

2. What is the difference between WPF Controls and WPF Content Controls?

A WPF Content Control is the Control that can hold and display a piece of content.

For example: Button, RadioButton, CheckBox, Label, Tooltip, ScrollViewer, UserControl, Window etc.

Note: All the WPF Content Controls are derived from System.Windows.ContentControl Class.

3. What is the different between WPF Content Controls and Headered Content Controls?

Headered Content Controls are the Content Controls which have both content region and header region which can be used to display some sore of title.

For example: GroupBox, TabItem and Expander

Note: All the WPF Headered Content Controls are derived from HeaderedContentControl class.

4. What is the difference between WPF Content Controls and WPF Layout Containers?

WPF Content Controls can contain only a single nested element while layout containers can contain as many nested elements. You can also take a lot of elements in Content Controls but the condition is that all the elements should be wrapped in a single parent layout container like stackpanel or grid.

5. What is the difference between Label and Textblock in WPF?

Label and Textblock are used to display the content on the screen. The basic difference is that Label support mnemonics (shortcut keys) while Textblock does not. If you do not want shortcut features, you can simply use Textblock as it is lightweight and also supports text wrapping which label does not.

6. What is the different between RadioButton and CheckBox in WPF?

You can do grouping in RadioButton but not in CheckBoxes.

For example: If you place 3 radio buttons in a stack panel, all the radio buttons in that stack panel are grouped i,e you can select any one of them but that feature is not there in CheckBoxes.

Note: Button, RadioButton and CheckBox are derived from ButtonBase class. Further, RadioButton and CheckBox are derived from ToggleButton class.

7. What is the different between Tooltip and Popup in WPF?

There are many differences between WPF Tooltip and Popup:

A) You cannot put user interactive controls in Tooltip as it cannot accept focus but same is possible in Popup.

B) Tooltip is opened and closed automatically when mouse is hovered but Popup does not.

8. How will you get the list of all the fonts installed on your system in WPF?

I will use static SystemFontFamilies collection defined in the System.Windows.Media.Fonts class.

foreach (FontFamily fontFamily in Fonts.SystemFontFamilies)
{
  lstFonts.Items.Add(fontFamily.Source);
}

9. What is the lexicon file in WPF? What is its use?

Lexicon file with exetension .lex is used to create customize the dictionary which is used for spell checking feature in WPF. In your WPF application, when you enable spell checking feature, WPF uses its default dictionary to recognize the words. If you want to add some extra words to that dictionary, you will have to create a lexicon file and add your words there. WPF supports dictionaries in English, German, Spanish and French.

10. What is the difference between Resource Files and Content Files in WPF?

1. Resource files are embedded into the assembly (exe or dll) while content files do not embed with assembly.

2. Content files are used instead of resource files in following scenarios:

A) You want to change the resource file without recompiling the application.
B) The resource file is very large.
C) The resource file is optional and may not be deployed with the assembly.
B) The resource file is a sound file.

How to make a resource file? Just add a file (say any image file) and set its Build Action to Resource (which is by default) in Properties Window.

How to make a content file? Just add a file (say any image file) and set its Build Action to Content in Properties Window. Set Copy to Output Directory to Copy Always.

For example: Suppose you have added a .jpg file of 30KB to your application as a resource. After you Build your application, you will notice an increase of approximately 30KB size in your exe. But if you use same file as content, there will be no increase in the size of exe, but you will notice that same file will be copied in the exe directory.

Related articles: I have written two posts on commonly asked WPF questions and answers. If you are looking for something like that, you can visit Part 1 and Part 2.

5 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete