Windows Application Using WPF


Step 1: Create Windows Application

Step 2: Right click on project click on Add=> NewItems

Image1.gif

Select User Control (WPF) Give name WPFUserControl.xaml Click on Add Button.

Desigine form using Tool Box.

This will Generate: 

<UserControl x:Class="WPFINWindowsApplication.WPFUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid>
        <Label Height="22" HorizontalAlignment="Left" Margin="22,16,0,0" Name="lblName" VerticalAlignment="Top" Width="82">Name</Label>
        <Button Height="23" Margin="65,55,0,0" Name="btnSave" VerticalAlignment="Top" HorizontalAlignment="Left" Width="75">Save</Button>
        <TextBox Height="23" Margin="90,18,90,0" Name="textBox1" VerticalAlignment="Top" />
    </Grid>
</
UserControl>

See Following Image

Image2.gif

Step 3: Add Event To Save Button Done Following Steps

Image3.gif

Image4.gif

If you Select NewEvent handler it will create event in backend file ( WPFUserComtrole.xaml.cs) as Shown in Following Figure

  <Button Height="23" Click="btnSave_Click"  Margin="65,55,0,0" Name="btnSave" VerticalAlignment="Top" HorizontalAlignment="Left" Width="75">Save</Button>

Image5.gif

Create Table in SqlServer Database by Running Following Script

USE [master]
GO
/****** Object:  Table [dbo].[WPFTest]    Script Date: 07/30/2010 15:29:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[WPFTest](
      [id] [int] IDENTITY(1,1) NOT NULL,
      [Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
 
GO
SET ANSI_PADDING OFF


Write Code To Save Name in database to btnSave_Click Event.

            SqlConnection m_SqlConnection = new SqlConnection("Data Source=KUSTERS-PC-06;Initial Catalog=master;User ID=sa;Password=adminadmin");
            m_SqlConnection.Open();
            SqlCommand m_SqlCommand = new SqlCommand("INSERT INTO [master].[dbo].[WPFTest] ([Name])VALUES ('" + textBox1.Text+"')",m_SqlConnection);
            m_SqlCommand.ExecuteNonQuery();
MessageBox.Show("Data Saved Sucessfully");

STEP 4:

REBUILD Apllication And Open Windows Form.

After rebuilding application We find WPFusercontrole Following location As show follow.

Image6.gif

Only you have to drag and drop control in windows Form as shown follows

Image7.gif

Run the windows form.

Output :

Image8.gif
 


Similar Articles