Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

September 13, 2010

Application Level Data Binding in WPF

 

Open VS2010 Navigate to File->New Project ->Windows->WPF Application-> Give a name to the project. App.xaml and mainwindow.xaml files are created by default in the project.

WPF Project

Mainwindow.xaml.cs file is the main file where we write our code. App.xaml is used for storing the connection strings in the application.

XAML is Extensible Application Markup Language that is used for designing WPF, Silverlight applications .

Design the UI

This is the code to be written in XAML file

<Window x:Class="WpfApplication1.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 x:Name="ownerdetails" Background="Azure" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>         <TextBlock x:Name="txtCity" Margin="67,8,84,10" Grid.Row="2" TextWrapping="Wrap"  />
        <TextBlock x:Name="txtLName" Margin="67,8,84,10" Grid.Row="1" TextWrapping="Wrap"  />
        <TextBlock x:Name="txtFName" Margin="67,8,84,10" TextWrapping="Wrap"  />
        <TextBlock x:Name="txtDOB" Margin="67,8,84,10" Grid.Row="3" TextWrapping="Wrap" />
        <TextBlock x:Name="txtCountry" Margin="67,8,84,10" Grid.Row="4" TextWrapping="Wrap" />
        <TextBlock x:Name="txtOwnerID" Margin="67,8,84,10" Grid.Row="5" TextWrapping="Wrap"  />
        <TextBlock x:Name="valOwnerFName" Grid.Column="1" Margin="69,8,72,10" TextWrapping="Wrap" Text="First Name" />
        <TextBlock x:Name="valOwnerLName" Grid.Column="1" Margin="69,8,72,10" Grid.Row="1" TextWrapping="Wrap" Text="Last Name" />
        <TextBlock x:Name="valCity" Grid.Column="1" Margin="69,8,72,10" Grid.Row="2" TextWrapping="Wrap" Text="City" />
        <TextBlock x:Name="valOwnerBirthdate" Grid.Column="1" Margin="69,8,72,10" Grid.Row="3" TextWrapping="Wrap" Text="DOB" />
        <TextBlock x:Name="valownerbalance" Grid.Column="1" Margin="69,8,72,10" Grid.Row="4" TextWrapping="Wrap" Text="Owner Balance" />
        <TextBlock x:Name="valownerID" Grid.Column="1" Margin="69,8,72,10" Grid.Row="5" TextWrapping="Wrap" Text="Owner ID" />
        <Image  HorizontalAlignment="Left"  Name="image1" Stretch="Uniform"  VerticalAlignment="Bottom"  Grid.Column="1" Source="images/hima.jpg" Grid.Row="6" />
    </Grid>
</Window>

Create a class to define properties of object

Add new item (class) to the project and define userdetails class that implements following properties.

class UserDetails
    {
        public int OwnerID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
        public string ImageName { get; set; }
        public DateTime BirthDate { get; set; }
    }

Write the code in MainWindow.cs file

MainWindow.xaml.cs file define these following methods

//Define user Details

private void GetUserDetails()
       {
           userdetails = new UserDetails();
           userdetails.OwnerID = 12345;
           userdetails.FirstName = "hima";
           userdetails.LastName = "Vejella";
           userdetails.ImageName = "images/hima.jpg";
           userdetails.State = "AP";
           userdetails.City = "Hyderabad";
           userdetails.Country = "India";
           userdetails.BirthDate = new DateTime(08 / 28 / 1981);

       }

//Set the values
       private void SetValues()
       {

           txtCity.Text = userdetails.City.ToString();
           txtDOB.Text = userdetails.BirthDate.ToString();
           txtLName.Text = userdetails.FirstName;
           txtFName.Text = userdetails.LastName;
           txtOwnerID.Text = userdetails.OwnerID.ToString();
           txtCountry.Text = userdetails.Country.ToString();

       }

Call the above two methods in the Main window.

UserDetails userdetails;
       public MainWindow()
       {
           InitializeComponent();
           GetUserDetails();
           SetValues();           
       }

Output

you can view thw output in a window as follows.

Output 

November 18, 2009

Speech Recognition name space in WPF

Recently I was working on an application where I need to develop Text to Speech in a Windows Application. This application is for physically blind people where when they run the application, it need to read the text file, and convert it into voice, and should be able to listen to the voice if speakers / headphones are connected.

Thanks to WPF, it has been so simple one line of code using Speech Synthesis in WPF.All the coding is achieved by importing System.Speech.Synthesis;

If you are using VS2005, you need to install WPF(Visual Studio Extensions for .NET Framework 3.0) as sperate template
When ever we install VS2008 all these libraries comes along with installation of .NetFramework 3.5.The conculsion here is the framework versions that support the speech synthesis is 3.0 and above (ie 3.0,3.5 and 4.0)

First thing is to import the name space using System.Speech.Synthesis;
This name space is comes long with the dll System.Speech
If you don’t add that reference you get error as the type or namespace name System.Speech does not exist in the namespace system.




To include the dll go to add references and include System.Speech

public partial class SpeechObj : Window
{
SpeechSynthesizer speechObject = new SpeechSynthesizer();

public SpeechObj()
{


InitializeComponent();
}

private void btnDisplayAudio_Click(object sender, RoutedEventArgs e)
{

speechObject.SpeakAsync(TxtAreaResult.Text);

}
}

July 09, 2007

.NET Framework 3.0

ASPAlliance publishes my article on All about .NET Framework 3.0
I have written an article and submitted in ASPAlliance .
Today I am happy to see the mail that it has got approved and published.

About ASPAlliance
It’s the site that connects Active Software Professionals. Here is the LINK
This article explains about Windows Presentation Foundation,Windows Communication Foundation,Windows WorkFlow Foundation in depth.

If you have find sometime please have a look at the ARTICLE

If you feel that article is useful please kick on the link here
kick it on DotNetKicks.com


AuctionAds: Increase the bid on your site's income!