Windows Presentation foundation

Note 


Frame can not navigate the pages using ICommand .

References:

https://www.tutorialspoint.com/wpf/wpf_overview.htm

WPF stands for Windows Presentation Foundation. It is a powerful framework for building Windows applications. This tutorial explains the features that you need to understand to build WPF applications and how it brings a fundamental change in Windows applications.
WPF was first introduces in .NET framework 3.0 version, and then so many other features were added in the subsequent .NET framework versions.

WPF Architecture

Before WPF, the other user interface frameworks offered by Microsoft such as MFC and Windows forms, were just wrappers around User32 and GDI32 DLLs, but WPF makes only minimal use of User32. So,
  • WPF is more than just a wrapper.
  • It is a part of the .NET framework.
  • It contains a mixture of managed and unmanaged code.
The major components of WPF architecture are as shown in the figure below. The most important code part of WPF are −
  • Presentation Framework
  • Presentation Core
  • Milcore
WPF Architecture





The presentation framework and the presentation core have been written in managed code. Milcore is a part of unmanaged code which allows tight integration with DirectX (responsible for display and rendering). CLR makes the development process more productive by offering many features such as memory management, error handling, etc.

WPF – Advantages

In the earlier GUI frameworks, there was no real separation between how an application looks like and how it behaved. Both GUI and behavior was created in the same language, e.g. C# or VB.Net which would require more effort from the developer to implement both UI and behavior associated with it.
In WPF, UI elements are designed in XAML while behaviors can be implemented in procedural languages such C# and VB.Net. So it very easy to separate behavior from the designer code.
With XAML, the programmers can work in parallel with the designers. The separation between a GUI and its behavior can allow us to easily change the look of a control by using styles and templates.

WPF – Features

WPF is a powerful framework to create Windows application. It supports many great features, some of which have been listed below −

FeatureDescription
Control inside a ControlAllows to define a control inside another control as a content.
Data bindingMechanism to display and interact with data between UI elements and data object on user interface.
Media servicesProvides an integrated system for building user interfaces with common media elements like images, audio, and video.
TemplatesIn WPF you can define the look of an element directly with a Template
AnimationsBuilding interactivity and movement on user Interface
Alternative inputSupports multi-touch input on Windows 7 and above.
Direct3DAllows to display more complex graphics and custom themes

Microsoft provides two important tools for WPF application development.
  • Visual Studio
  • Expression Blend
Both the tools can create WPF projects, but the fact is that Visual Studio is used more by developers, while Blend is used more often by designers.
XAML:
XAML stands for Extensible Application Markup Language. It’s a simple and declarative language based on XML.
  • In XAML, it very easy to create, initialize, and set properties of objects with hierarchical relations.
  • It is mainly used for designing GUIs, however it can be used for other purposes as well, e.g., to declare workflow in Workflow Foundation.

<Window x:Class = "Resources.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>

InformationDescription
<WindowIt is the opening object element or container of the root.
x:Class = "Resources.MainWindow"It is a partial class declaration which connects the markup to the partial class code defined behind.
xmlns = "http://schemas.microsoft.com/win fx/2006/xaml/presentation"Maps the default XAML namespace for WPF client/framework
xmlns:x = "http://schemas.microsoft.com/w infx/2006/xaml"XAML namespace for XAML language which maps it to x: prefix
>End of object element of the root
<Grid>
</Grid>
It is starting and closing tags of an empty grid object.
</Window>Closing the object element

The syntax rules for XAML is almost similar to XML. If you look at an XAML document, then you will notice that it is actually a valid XML file, but an XML file is not necessarily an XAML file. It is because in XML, the value of the attributes must be a string while in XAML, it can be a different object which is known as Property element syntax.

References:

WPF - Elements Tree

 Windows Presentation Foundation (WPF) has a comprehensive tree structure in the form of objects. In WPF, there are two ways that a complete object tree is conceptualized −
  • Logical Tree Structure
  • Visual Tree Structure

    Logical Tree Structure

    In WPF applications, the structure of the UI elements in XAML represents the logical tree structure. In XAML, the basic elements of UI are declared by the developer. The logical tree in WPF defines the following −
    • Dependency properties
    • Static and dynamic resources
    • Binding the elements on its name etc.

      Visual Tree Structure

      In WPF, the concept of the visual tree describes the structure of visual objects, as represented by the Visual Base Class. It signifies all the UI elements which are rendered to the output screen.
      When a programmer wants to create a template for a particular control, he is actually rendering the visual tree of that control. The visual tree is also very useful for those who want to draw lower level controls for performance and optimization reasons.
      In WPF applications, visual tree is used for −
    • Rendering the visual objects.
    • Rendering the layouts.
    • The routed events mostly travel along the visual tree, not the logical tree.

  • The logical tree leaves out a lot of detail enabling you to focus on the core structure of the user interface and to ignore the details of exactly how it has been presented.
  • The logical tree is what you use to create the basic structure of the user interface.
  • The visual tree will be of interest if you're focusing on the presentation. For example, if you wish to customize the appearance of any UI element, you will need to use the visual tree.

WPF - Dependency Properties 


In WPF applications, dependency property is a specific type of property which extends the CLR property. It takes the advantage of specific functionalities available in the WPF property system.
A class which defines a dependency property must be inherited from the DependencyObjectclass. Many of the UI controls class which are used in XAML are derived from the DependencyObject class and they support dependency properties, e.g. Button class supports the IsMouseOver dependency property.

Why We Need Dependency Properties

Dependency property gives you all kinds of benefits when you use it in your application. Dependency Property can used over a CLR property in the following scenarios −
  • If you want to set the style
  • If you want data binding
  • If you want to set with a resource (a static or a dynamic resource)
  • If you want to support animation
Basically, Dependency Properties offer a lot of functionalities that you won’t get by using a CLR property.
The main difference between dependency properties and other CLR properties are listed below −
  • CLR properties can directly read/write from the private member of a class by using getter and setter. In contrast, dependency properties are not stored in local object.
  • Dependency properties are stored in a dictionary of key/value pairs which is provided by the DependencyObject class. It also saves a lot of memory because it stores the property when changed. It can be bound in XAML as well.

Custom Dependency Properties

In .NET framework, custom dependency properties can also be defined. Follow the steps given below to define custom dependency property in C#.
  • Declare and register your dependency property with system call register.
  • Provide the setter and getter for the property.
  • Define a static handler which will handle any changes that occur globally
  • Define an instance handler which will handle any changes that occur to that particular instance.                                                                                                                                  Example: pending

WPF - Routed Events


routed event is a type of event that can invoke handlers on multiple listeners in an element tree rather than just the object that raised the event. It is basically a CLR event that is supported by an instance of the Routed Event class. It is registered with the WPF event system. RoutedEvents have three main routing strategies which are as follows −
  • Direct Event
  • Bubbling Event
  • Tunnel Event

Direct Event

A direct event is similar to events in Windows forms which are raised by the element in which the event is originated.
Unlike a standard CLR event, direct routed events support class handling and they can be used in Event Setters and Event Triggers within your style of your Custom Control.
A good example of a direct event would be the MouseEnter event.

Bubbling Event

A bubbling event begins with the element where the event is originated. Then it travels up the visual tree to the topmost element in the visual tree. So, in WPF, the topmost element is most likely a window.

Tunnel Event

Event handlers on the element tree root are invoked and then the event travels down the visual tree to all the children nodes until it reaches the element in which the event originated.
The difference between a bubbling and a tunneling event is that a tunneling event will always start with a preview.
In a WPF application, events are often implemented as a tunneling/bubbling pair.

Key concepts of WPF

References: 

1.Binding :
Binding establishes the connection between the application and the business layers.

you want to bind the content of a TextBox to the Name property of an Employee object, your target object is the TextBox, the target property is the Text property, the value to use is Name, and the source object is the Employee object.

Binding in XAML provides ultimate access to the definition interface that connects the properties of binding target objects and any data source typically binding elements may be a WPF element and the data source can be a database, an XML file, or any object that contains data.


A Binding object connects a dependency property of a FrameworkElement directly to a data object so that updates to the data object are automatically propagated to the property that uses data binding. The Binding class defines the properties of a binding. Each binding must have a target element, target property, and data source, although some values are provided by default if you don't specify them.


Target Object :- WPF control like TextBox etc.

Source Object:- class object

Data binding:

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data.

Data binding is a process in which you bind data to a control. It is similar to creating a connection between the back-end code and model, to the user-interface
References

Binding Members

  1. Source: The source property holds the DataSource. By default, it references the DataContext of the control. If you place Source property for the Binding, it will take that in lieu of original DataContext element.
  2. ElementName: In case of Binding with another ElementElementName takes the name of the Element defined within the XAML for reference of the object. ElementName acts as a replacement to Source. If path is not specified for the Binding, it will use ToString to get the data from the Object passed as Source.
  3. PathPath defines the actual property path to get the String Data. If the end product is not a string, it will also invoke ToString to get the data.
  4. Mode: It defines how the Data will be flown. OneWay means object will be updated only when source is updated, on the contrary OneWayToSource is the reverse. TwoWay defines the data to be flown in both ways.
  5. UpdateSourceTrigger: This is another important part of any Binding. It defines when the source will be updated. The value of UpdateSourceTrigger can be :
    • PropertyChanged: It is the default value. As a result, whenever anything is updated in the control, the other bound element will reflect the same.
    • LostFocus: It means whenever the property loses its focus, the property gets updated.
    • Explicit: If you choose this option, you need to explicitly set when to update the Source. You need to use UpdateSource of BindingExpression to update the control.
      BindingExpression bexp = mytextbox.GetBindingExpression(TextBox.TextProperty);
      bexp.UpdateSource();
      By this, the source gets updated.
  6. ConverterConverter gives you an interface to put an object which will be invoked whenever the Binding objects get updated. Any object that implements IValueConverter can be used in place of Converter. You can read more about it from Converter in Binding [^].
  7. ConverterParameter: It is used in addition to Converter to send parameters to Converter.
  8. FallbackValue: Defines the value which will be placed whenever the Binding cannot return any value. By default, it is blank.
  9. StringFormat: A formatting string that indicates the Format to which the data will follow.
  10. ValidatesOnDataErrors: When specified, the DataErrors will be validated. You can use IDataErrorInfo to run your custom Validation block when Data object is updated. 

References :

Static Resources
Dynamic Resources

Static Resource - Static resources are the resources which you cannot manipulate at runtime. 
The static resources are evaluated only once by the element which refers them during the loading of XAML. 

 Dynamic Resource - Dynamic resources are the resources which you can manipulate at runtime and are evaluated at runtime.That's because the DynamicResource markup extension can only be used on a dependency property, because it will need to update it if the resource changes. 



Style : 
ResourceDictionary:
https://docs.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/resourcedictionary-and-xaml-resource-references

Resources are typically definitions of some object that you expect to use more than once.
XAML resources are objects that are referenced from markup more than once. Resources are defined in a ResourceDictionary, typically in a separate file or at the top of the markup page.
A resource dictionary is a repository for XAML resources, such as styles, that your app uses. You define the resources in XAML and can then retrieve them in XAML using the {StaticResource} markup extension and {ThemeResource} markup extension s.

Step to implement resource dictionary

1. add item-> Resource dictionary (in any separate folder)

2. define style  in  resource dictionary xaml file

3.  now set in page/window.resources in page

Page/Window level
<Page.Resources>
 <ResourceDictionary x:Key = "brushResource" Source = "/ResourceDictionaries/DictionaryWithBrush.xaml"/>      

    </Page.Resources>
Application Level

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
           <ResourceDictionary source="ResourceDictionaries/DictionaryWithBrush.xaml"/>               </ResourceDictionary.MergedDictionaries>           
        </ResourceDictionary>

    </Application.Resources>


4. u can use static /dynamic resources










Comments

Popular posts from this blog

Asp .Net Core

Microsoft Enterprise Library-Data Access Application Block for for .Net Core & .Net Standard

Asp .Net Core -Startup Class and the Service Configuration