Search This Blog

Tuesday, January 15, 2013

Simple Example WPF DATA Grid:



 
    The DataGrid is commonly used window form control,when we try to learn WPF better to learn by actual 
starting with something we can relate with like datagrid control.
below is quick way to use datagrid in wpf.

Here I am binding grid to a Generics based collection.Latter on we will move to bind grid with data from sql server and xml using LINQ to  xml or linq to sql.

Generics in csharp  is somewhat  similar to templated class in C++.

Create a wpf application .add a class to it name it student.cs

Create a class here as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WpfControlDemo.Entity
{
    public class Student
    {

    public int id { get; set; }

    public string Fname { get; set; }

    public string Lname { get; set; }

    public DateTime AdmissionDate { get; set; }

    public string Gender { get; set; }

    private int Rating { get; set; }

    public bool NeedPlacement { get; set; }

    }
}

Now go to your MainWindow.xaml
Right click on here and select “view code”, in code window add this method to class
private List<Student> LoadCollectionData()
        {

            List<Student> Students = new List<Student>();

            Students.Add(new Student()
            {

                id = 1,
                Fname = "sangram",
                Lname = "desai",
                AdmissionDate = new DateTime(1975, 2, 23),
                Gender = "Male",
                NeedPlacement = true
            });

            Students.Add(new Student()
            {
                id = 2,
                Fname = "sagar",
                Lname = "desai",
                AdmissionDate = new DateTime(1982, 4, 12),
                Gender = "Male",
                NeedPlacement = true

            });

            Students.Add(new Student()
            {

                id = 3,
                Fname = "sachin",
                Lname = "desai",
                AdmissionDate = new DateTime(1982, 5, 12),
                Gender = "Male",
                NeedPlacement = false

            });
            Students.Add(new Student()
            {

                id = 4,
                Fname = "mayuri",
                Lname = "shirvankar",
                AdmissionDate = new DateTime(1982, 5, 12),
                Gender = "Female",
                NeedPlacement = true

            });

            Students.Add(new Student()
            {

                id = 5,
                Fname = "kirti",
                Lname = "shirvankar",
                AdmissionDate = new DateTime(1982, 5, 12),
                Gender = "Female"

            });

            return Students;

        }
You may get an error as student class is not available add

using WpfControlDemo.Entity;

in proper place

This method create a collection of student using Generics.

Now switch to design view of MainWindow.xaml add a DataGrid I am naming it as ‘dg1
And add a button my button name is LoadData1

Then change content property of Button to “Load data”,now click the button to generate corresponding event handler LoadData1_Click in code window of MainWindow.xaml

<Window x:Class="WpfControlDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="496">

    <Grid Height="331" Width="628">
       <DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left"     
       Margin="10,10,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" />

      <Button Content="LoadData" Height="23" HorizontalAlignment="Left"  
      Margin="213,221,0,0" Name="LoadData1" VerticalAlignment="Top" Width="75"    
      Click="LoadData1_Click" />
</Grid>
</Window>


Now modify your event handler for button click like follows
  private void LoadData1_Click(object sender, RoutedEventArgs e)
        {
            dg1.ItemsSource = LoadCollectionData();
        }

By default DataGrid’s auto generate column property is false set it to true so that datagrid markup look like
    <Grid Height="331" Width="628">
       <DataGrid AutoGenerateColumns="True" Height="200" HorizontalAlignment="Left"     
       Margin="10,10,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" />

Compile code and run Here you observe that all public properties of student  get bind in datagrid.
You can resize column width,sort rows,re arrange column order by dragging columns select a row at a time all these are due to default grid properties that one can customize using it’s attributes

<DataGrid AutoGenerateColumns="True" Height="185" HorizontalAlignment="Left" Margin="10,10,0,0" Name="dg1" VerticalAlignment="Top" Width="460" RowHeight="30" ColumnWidth="90" GridLinesVisibility="Vertical" HeadersVisibility="All" Background="LightGray"
        RowBackground="LightYellow" BorderBrush="Gray" BorderThickness="5" AlternatingRowBackground="LightBlue" IsReadOnly="True" CanUserReorderColumns="False"
        CanUserResizeColumns="False" CanUserSortColumns = "False" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" SelectionMode="Extended"/>

Here I set  SelectionMode  to Extended so that one can select more than just a row. We used RowBackground & AlternatingRowBackground pair of attribute to give different background color to alternate rows.Other relevant properties are Height,Width.
BorderBrush is border color in asp.net sense while BorderThickness is Border width.
CanUserReorderColumns is used to prevent user from re arranging columns in grid by dragging one over other. CanUserResizeColumns is used to prevent user from resizing column width.
Same way we can restrict user from sorting column by adding CanUserSortColumns=’False’

There are lot of attributes of datagrid that we can use to customize look and fill & it's behaviour.

 

Tuesday, January 8, 2013

What Is WPF?


Windows Presentation Foundation (WPF) could be termed the next generation of Windows user

Interfaces. It could be, but there are too many companies that will be wedded to Windows Forms for

Many years to come. It may be more accurate to call WPF an alternative to Windows Forms. Although

the two technologies differ in many ways, they both aim to achieve the same end: providing a user

interface on the Windows desktop for applications written using the .NET Framework.



Architecture



Figure above shows the general architecture of WPF and its constituent parts.

The three subsystems that are the newly added ingredients that form WPF are



1) Presentation Framework:

Presentation Framework is the subsystem that contains the entire user interface

components. It has a rich library of controls that can be used by WPF

applications. You can think of your WPF application as sitting on top of all of

this architecture, leveraging the functionality of the .NET Framework and WPF

to produce something usable and, ideally, profitable.

2) Presentation Core: Presentation Core contains all of the classes and interfaces that form the groundwork for WPF. It does not contain any user interface controls—it is more

like the foundations that the controls are built on.



3) MIL Core: MIL Core is unmanaged, which enables it to wrap DirectX much more closely.

MIL Core stands for Media Integration Layer. It is an unmanaged wrapper

around DirectX and allows the Common Language Runtime (CLR) to interface with DirectX.



4) Kernel: The Kernel provides low-level operating system services such as primitive input and output (I/O), memory management, and process/thread creation and synchronization functions.

 

5) User32:User32 provides applications with the means to create graphical user interfaces (GUIs) by using the message queue, creating and managing windows, and so forth.



6) DirectX provides an Application Programming Interface (API) for creating 3D graphics, either using software rendering or via 3D graphics accelerator cards.



7) CLR: The Common Language Runtime (CLR) is Microsoft’s implementation of the Common Language

Infrastructure (CLI) standard: C# and Visual Basic .NET code are actually compiled into Common

Intermediate Language (CIL) bytecode, which is compiled again by the CLR at runtime before being

executed. All .NET languages operate this way and sit on top of the CLR.


WPF & WINDOW FORM Comparison
 1)WPF applications are graphically superior to Windows Forms applications; WPF user interfaces often look more compelling than their Windows Forms counterparts.
2)WPF applications have a higher minimum hardware—and, in fact, software—requirement than Windows Forms applications.
3) Graphically superior means that 3D is supported; cutting-edge graphical rendering techniques can be employed and any hardware acceleration present in the machine that is running the application will be leveraged.
4) WPF applications will run only on a minimum of Windows XP Service Pack 2
5) Windows Forms is used to target operating systems such as Windows 98 or 2000
6) WPF is built on DirectX unlike Window form which used GDI+.



What Is GDI?


The Graphics Device Interface (GDI) API is contained within the GDI32.dll that ships with the Windows operating system, and has been deprecated since Windows XP.

   It provides facilities for low-level drawing of lines and curves, font rendering, and other menial graphical tasks that are expanded upon in other APIs, such as User32.


    Since Windows XP,GDI has been superseded by GDI+, which is a C++ implementation of a similar primitive graphics layer but adds support for extra features such as gradient shading and for JPEG and PNG graphics files.

    Much of the System.Drawing namespace and its container, the System.Drawing.dll assembly, is a managed code wrapper around GDI+.



What is DirectX?


DirectX is an API built for graphically intensive applications such as Computer Aided Design (CAD)

Applications or video games. It provides an interface for rendering graphical scenes represented in three dimensions onto a two-dimensional projection, such as computer monitor.

 As managed .NET code can’t use unmanaged APIs directly, WPF uses a wrapper around DirectX as an intermediary.

MIL Core provides a low-level interface to the DirectX API, and WPF builds on top of this to perform all of its graphical rendering.




How To Target both WPF & Silverlight:

1) Separate Assembly for both
2) Partial Classes(introduced in .Net 2.0)
3)Extension Method(introduced in .Net 3.0)
4) Compiler Directives




What is XAML?


  Extensible Application Markup Language, or XAML (pronounced "zammel"), is an XML-based markup language developed by Microsoft. XAML is the language behind the visual presentation of an application.

     To solve a particular problem Object-oriented paradigm combine data with methods that

Operate on the it. Windows Forms makes use of this paradigm, building the user interface on top of object-oriented code. Whenever you use the Windows Forms designer, the underlying functionality is still provided by

Object-oriented code that instantiates control classes and sets their properties to reflect your design.

   XAML uses a declarative programming paradigm, which allows developers to lay out their user interface without any references to control flow this means
1) XAML is more intuitive than Windows Forms code, because it is specialized for the purpose

of describing a user interface.
2) It separates the user interface declaration from its code counterpart.



XAML, the XML-based markup language that allows developers to create user interfaces declaratively, rather than programmatically.