Posts

Showing posts with the label WPF

WPF Page Hosted Windows Applications and XAML Browser Applications (XBAPs)

WPF Page Hosted Windows Applications and XAML Browser Applications (XBAPs) In WPF, you can also make page based applications. These page based applications are of two types: 1. Pages Hosted in Windows Application 2. Stand-alone XAML Browser Application(XBAPs) Lets try to understand these WPF Page based applications: 1. Pages Hosted in Windows Application In this approach, you create your windows application and host some of the pages in that Windows application using NavigationWindow or Frame. This approach is generally used in WPF applications to integrate some kind of wizard in your application, to show some help contents and documentation. Hosting Pages in Navigation Window System.Windows.Controls.Page class is used as top level container instead of Windows.  <Page...> ... ... </Page> But actually, there is one more top level container known as NavigationWindow (derives from Windows class) in which Page container is hosted. The NavigationWindow looks like an ordinary...

Localization and Satellite Assemblies in WPF: How to support Localization in your WPF application?

Localization and Satellite Assemblies in WPF: How to support Localization in your WPF application? In WPF applications, the unit of localization is the XAML file (technically, the compiled BAML resource that is embedded in your application). If you want to support three languages, you need to include three BAML resources. Satellite assemblies play an important role in localization of WPF applications. When you create a localized WPF application, you place each localized BAML resource in a separate satellite assembly. To add localization support to your WPF application, just go to your .csproj file and add <UICulture>en-US</UICulture> under <PropertyGroup> element. Now Build your project and you will get a folder named en-US created where your exe is placed.  If you go inside this folder, you will get an assembly with the same name as that of your exe but with different extensions. For example, if your application exe name is MySampleApplication.exe , its name ...

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 appear...

WPF XAML: Property-Attribute and Property-Element Syntax for Simple and Complex Properties

WPF XAML: Property-Attribute and Property-Element Syntax for Simple and Complex Properties Property-Attribute and Property-Element are the two approaches in WPF XAML to set the properties of the elements. I will take a very simple example to clear the concepts of both of the Property-Attribute and Property-Element approaches. Lets try to set the background property of the grid. I want to fill the background of the grid with some gradient color. Lets see how can we do this using XAML in WPF? Suppose I have to set the background of the grid to Green. I will set it like this: <Grid Name="MyGrid" Background="Green" > .... .... </Grid> In the above case, I have used Property-Attribute syntax for setting the Background property. WPF has used default brush and painted the area with solid color. But what if you want to use some complex brush and want to paint the background with gradients instead of simple solid color?  In this scenario, you will have to use Pro...

WPF XAML Namespaces, Prefixes and Conventions

WPF XAML Namespaces, Prefixes and Conventions In WPF, XAML contains namespaces for defining controls for your XAML document. You can also map other .NET namespaces to XML namespaces and use them in XAML document. There are some conventions for using prefixes with these namespaces in XAML. Lets go point to point to explore everything about XAML namespaces, prefixes used with namespaces and conventions for using prefixes in XAML. <Window x:Class="SampleApplication.MainWindow"   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   Title="MainWindow" Height="350" Width="525">   <Grid>          </Grid> </Window> 1. xmlns  attribute is a specialized attribute in the world of XML that is reserved for declaring namespaces. 2. The above default XAML code contains two XAML namespaces.  http://schemas.microsoft.com/winfx/2006/xaml/presentati...

WPF XAML, Loose XAML and BAML Files: Approaches to create WPF applications with XAML

WPF XAML, Loose XAML and BAML Files: Approaches to create WPF applications with XAML There are four approaches to create WPF application with XAML. You can use XAML directly to in WPF application or can convert XAML into the BAML for improving its performance or you can use loose XAML or do not use XAML at all. Lets try to understand these approaches of creating WPF applications with XAML. Code and compiled markup(BAML): This is the preferred approach for WPF applications. This approach is used by Visual Studio. You can create XAML template for each window, and this XAML is compiled into BAML and embedded in a final assembly. At runtime, the compiled BAML is extracted and is used to generate the user interface. Code and uncompiled markup(XAML): You can use this approach when you are creating highly dynamic user interface. In this approach, you load the user interface from a XAML file at runtime using the XamlReader class from the System.Windows.Markup namespace. Code only: This is t...

WPF Layout Controls/Containers, Layout Process, Properties and Tips

WPF Layout Controls/Containers, Layout Process, Properties and Tips All the WPF Layout containers are panels that derive from the abstract System.Windows.Controls.Panel class. Mainly, there are five layout containers in WPF: 1. StackPanel 2. WrapPanel 3. DockPanel 4. Grid 5. Canvas I am not going to explain working of all these panels as it is already very well explained here . I will add some extra details about these layout controls in WPF. I will go through WPF Layout Process Stages ( Measure Stage and Arrange Stage ), how can we create our custom layout containers using MeasureOverride() and ArrangeOverride() methods, different WPF layout properties and some tip and noteworthy points about WPF layout containers. WPF Layout Process WPF Layout takes place in two stages: A) Measure Stage: The container loops through its child elements and asks them to provide their preferred size. B) Arrange Stage: The container places the child elements in the appropriate position. Creating WPF C...

WPF XAML and BAML: XAML Introduction, Advantages and Compilation

WPF XAML and BAML: XAML Introduction, Advantages and Compilation This article on WPF XAML covers basic concepts of XAML, Advantages of using XAML into WPF, how XAML elements are related with .NET classes and how XAML in actually compiled in WPF applications. We will get an idea on how WPF XAML code is converted into the BAML and again how BAML code is converted into the XAML and the role of InitializeComponent() method in the process. XAML stands for Extensible Application Markup Language and is mainly used to create WPF user interface. XAML is also used in Silverlight and WF (Windows Workflow Foundation). You can create XAML using Visual Studio as well as Expression Blend. Basically Visual Studio is for developers and Expression Blend is for designers. Advantage of XAML in WPF applications In WPF, due to the introduction of XAML, graphical designer can separately work on user interface and developers can concentrate on the main logic. But it was not possible in Windows Form because an...

WPF Introduction, Architecture and Core Classes

Image
WPF Introduction, Architecture and Core Classes WPF is a modern graphical display system for Windows. WPF has innovative features like built-in Hardware Acceleration and Resolution Independence.  Before WPF, User32 and GDI/GDI+ components were used for creating user interface which had a lot of limitations. Windows Forms and VB6 used these User32 and GDI/GDI+ components for creating user interface. To overcome limitations of User32 and GDI/GDI+, DirectX was introduced. DirectX is the highly efficient toolkit of game development on Windows and has support for all modern video cards. WPF uses DirectX instead of GDI/GDI+. User32 is still used but its use by WPF is very limited. WPF 4.5 is compatible only with Windows Vista, Windows 7 and Windows 8. For Windows XP, you will have to configure Visual Studio to target .NET 4.0 framework rather than .NET 4.5. WPF Architecture WPF Architecture can be divided into three layers: 1. Managed WPF API Layer includes A) PresentationFramework.dll:...

WPF StaticResource and DynamicResource Examples and Explanations

WPF StaticResource and DynamicResource Examples and Explanations A WPF Resource is an object that can be reused in different places in your WPF application. Brushes and Styles are the best examples of WPF Resources. WPF Resources are not the part of visual tree but can be used in your user interface. Generally WPF objects are defined as resources, which are used by multiple elements of the application. WPF Resources are of two types: 1. Static Resource 2. Dynamic Resource 1. StaticResource: StaticResources are resolved at compile time. Use StaticResources when it's clear that you don't need your resource re-evaluated when fetching it static resources perform better than dynamic resources. Syntax for StaticResource usage:  <object property="{StaticResource key}" .../>  2. DynamicResource: DynamicResources are resolved at runtime. Use DynamicResources when the value of the resource could change during the lifetime of the application. Syntax for DynamicResource u...

Usage of ObservableCollection class and INotifyCollectionChanged interface in WPF Data Binding

Usage of ObservableCollection class and INotifyCollectionChanged interface in WPF Data Binding The ObservableCollection<T> is one of the most important features of WPF data binding. ObservableCollection is a generic dynamic data collection that provides notifications (using an interface "INotifyCollectionChanged") when items get added, removed, or when the whole collection is refreshed. Namespace for ObservableCollection: System.Collections.ObjectModel Syntax for ObservableCollection: [SerializableAttribute] public class ObservableCollection<T> : Collection<T>,  INotifyCollectionChanged, INotifyPropertyChanged ObservableCollection has a lot of properties, methods, events, explicit interface implementations. Get a complete list on MSDN . Main feature of the ObservableCollection<T> are the events it raises when the items it contains change. When in your WPF application, you make any changes like remove/add/edit item in the ObservableCollection, the Ob...