Thursday 20 September 2012

wpf



In .Net framework 3.0, Microsoft introduced foundation concepts WPF (Windows Presentation Foundation) is one of the foundation technology concept by Microsoft with .Net framework 3.0
  • WPF means Windows Presentation Foundation
  • WPF is one of the type of application using this WPF template we can develop a windows application
  • WPF application is an extension of traditional wing forms applications
  • Using WPF application user can develop a rich user interface
  • Microsoft mainly concentrated on user interface in WPF
  • WPF application is integrated with rich user interface and 2D or 3D graphics and multimedia features
  • Because of all these features, .Net programmer can develop a beautiful user interface by using WPF
  • WPF application is reducing the programmer task while design windows page
  • They have introduced some additional controls in WPF
  • To make programmer task easy they have changed some of the property names
  • To develop a WPF application we can use WPF application template within the Visual Studio
  • Base namespace for WPF application programming is System.Windows
  • WPF application will contain 2 code windows
Ø  .xmal
Ø  .xmal.cs
  • .XAML: .XMAL means Xtensible Application Markup Language.
  • XAML file contains design part code which was developing a markup language called XAML.
  • Using XAML. We can design the windows page. We can control appearance of window page.

            Code:
                                    <Window>
                                                <Grid>

                                                            ----
                                                </Grid>
                                    </Window>

XAML:
           
  • XAML is a markup language, is base on XML
  • XAML is introduced by Microsoft. It was invented for WPF to create and initialize WPF user interface

.XAML.cs:

  • This code window is representing business logic
  • Here we can use any .Net language to develop the application

WPF User Interface:

  • In traditional windows application user interface is called as Form
  • In WPF application user interface is called as Window.
  • Within that window we will have one more container called Grid
  • This Grid is used to design the WPF windows page with the controls.


Example for Simple WPF application:

Step 1: Open a WPF application using Visual Studio
           
            Name the window as employeeinformation


Step 3: Implement the below code on btndisplay_click event

                        Private void btndisplay_click(- -)
                        {
                                    textName.Text = “Shankar”;
                                    textDest.Text = “Software Engineer”;
                        }


Example: To retrieve the data from the database to display within the Grid view control of WPF window.

                        First create a database table with name emp

           
Empno
Ename
Sal
111
Shankar
7000
222
Pramod
8000
333
Laxman
9000

Step1: Goto SQL Server database and create a table

-         Create table emp(Empno int primary key, Ename varchar(15), Sal int)
-         Press F5 for execute
-         Insert into emp values(111, ‘Shankar’, 7000)
-         Insert into emp values(122, ‘Pramod’, 8000)
-         Insert into emp values(333, ‘Laxman’, 9000)

Step 2: Opening a WPF application using Visual Studio

            Note: Grid view control is not available in WPF application
           
-         Whenever we want to use Gridview control in WPF application we can create the Gridview control within the Listview control
-          
-         Drag and drop the listview control
-         S
-         Drag and drop the button control
-         S
-         Write the below code to create gridview control in .xaml file

Nt: Within the listview control we have to write the code

.xaml file:

            <Listview Margin=”48,60,0,102”, Name=”Listview1” HorizontalHeight =”Left”
                                                            Width=”203” itemsource=”{Binding path=Table}”>
            <Listview.view>
                        <Gridview>
                                    <Gridview column Header = “Empno”
                                                DisplayMemberBinding=”{Bindingpath=Empno}”>
                                    </Gridview column>

                                    <Gridview column Header = “EmpName”
                                                DisplayMemberBinding=”{Bindingpath=Ename}”>
                                    </Gridview column>


                                    <Gridview column Header = “Salary”
                                                DisplayMemberBinding=”{Bindingpath=Sal}”>
                                    </Gridview column>
                        </Gridview>
            </Listview.view>
</Listview>

 Code for button:

                        <Button Height = “23” Margin=”90,0,113,50”; Name=”Button”
VerticalAlignment= “Bottom” click=”button1_click”>Display
                        </Button>
                        </Grid>
                        </Window>
           
Write code in .xaml.cs file in btndisplay_click:

            Add namespaces:
                 
                  Using System.Data;
                  Using System.Data.Sqlclient;

                  public void button1_click(--)
                  {
                              string cs = “server=SHANKAR-PC; database=ltc_gamble; uid=sa; pwd=456321aA”;

                              sqlconnection conn = new sqlconnection(cs);

                              sqlcommand cmd = new sqlcommand(“Select * from emp”, conn);

                              sqlDataAdapter da = new sqlAdapter(cmd);

                              Dataset ds = new Dataset( );

                              da.Fill(ds, “empnew”);
     
                              listview1.Datacontrol = ds.Tables[0].Default.view;
                  }

Example for Inserting the data into a table within the SQL server using WPF application:

Step1: Open a WPF application using Visual Studio

      Drag and drop the 3 labels, 3 textboxes, and 1 button :

Step 2: Write below code within the btnInsert_click of .xaml.cs file

                  Using System.Data;
                  Using System.Data.Sqlclient;

                  Private void btninsert_click(--)
                  {
                              int empno = Covert.Toint32(texteno.Text);
                              string ename=textename.Text;
                              int sal = Convert.Toint32(textsal.Text);

                              string cs = “server=SHANKAR-PC; database=ltc_gamble; uid=sa; pwd=456321aA”;

                              sqlconnection conn = new sqlconnection(cs);

                              conn.open( );

sqlcommand cmd = new sqlcommand(“Insert into emp values(@empno, @ename, @sal)”, conn);

                              cmd.Parameters.Addwithvalue(“@empno”, empno);
                              cmd.Parameters.Addwithvalue(“@ename”, ename);
                              cmd.Parameters.Addwithvalue(“@sal”, sal);

                              cmd.ExecuteNonQuery( );

                              texteno.clear( );
                              textename.clear( );
                              textsal.clear( );

                              label.content = “Registration successfully”;

                  }
                              

No comments: