Add,Change and Delete a user using WPF

Pardhu2020
Posted by in WPF category on for Beginner level | Points: 250 | Views : 4313 red flag
Rating: 5 out of 5  
 1 vote(s)

Hi friends today we will discuss Add,Change,Delete functionality in WPF.

Introduction

Hi friends today we will discuss Add,Change,Delete functionality in  WPF



  1. Once open the Visual Studio click on File Click on the Project .

  1. As you can see there are many application(s) from them Select WPF application
  2. At name rename it as per your requirement .

  1. Right click on the project and click on add from there click on window for adding a new window to the project
  2. Rename the window as per your requirement

  1. Drag a Dockpanel from tools .
  2. Drag a three Button and rename it as showed above .
  3. Now its time to enable the functionality using the below code we can achieve it.
<Window x:Class="DataBindingExamples.Window3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window3" Height="300" Width="300">
    <DockPanel Margin="10">
        <StackPanel DockPanel.Dock="Right" Margin="10,0,0,0">
            <Button Name="btnAddUser" Click="btnAddUser_Click">Add user</Button>
            <Button Name="btnChangeUser" Click="btnChangeUser_Click" Margin="0,5">Change user
	    </Button>
            <Button Name="btnDeleteUser" Click="btnDeleteUser_Click">Delete user</Button>
        </StackPanel>
        <ListBox Name="lbUsers" DisplayMemberPath="Name"></ListBox>
    </DockPanel>
</Window>

Window3.xaml.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace DataBindingExamples
{
    /// <summary>
    /// Interaction logic for Window3.xaml
    /// </summary>
    public partial class Window3 : Window
    {
        private ObservableCollection<User> users = new ObservableCollection<User>();

        public Window3()
        {
            InitializeComponent();
            
            users.Add(new User() { Name = "Jojo" });
            users.Add(new User() { Name = "Popat Lal" });
            users.Add(new User() { Name = "Santa Singh" });
            users.Add(new User() { Name = "Banta Singh" });

            lbUsers.ItemsSource = users;
        }

        private void btnAddUser_Click(object sender, RoutedEventArgs e)
        {
            users.Add(new User() { Name = "Steve" });
        }

        private void btnChangeUser_Click(object sender, RoutedEventArgs e)
        {
            if (lbUsers.SelectedItem != null)
                (lbUsers.SelectedItem as User).Name = "Smith";
        }

        private void btnDeleteUser_Click(object sender, RoutedEventArgs e)
        {
            if (lbUsers.SelectedItem != null)
                users.Remove(lbUsers.SelectedItem as User);
        }
    }

    public class User:INotifyPropertyChanged
    {
        private String _name;

        public String Name
        {
            get
            {
                return this._name;
            }
            set
            {
                if (this._name != value)
                {
                    this._name = value;
                    this.NotifyPropertyChanged("Name");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(String propname)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propname));
            }
        }
    }
}


ADD



Change



Delete



Conclusion

In this article we have seen how to add user,Change user,Delete a user Using WPF.







Page copy protected against web site content infringement by Copyscape

About the Author

Pardhu2020
Full Name: Pardha Saradhi
Member Level:
Member Status: Member
Member Since: 3/19/2013 2:21:22 PM
Country: India

http://www.dotnetfunda.com

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)