WPF MVVM JSON Operations (Part - I : Read Json File Data to List and Show on Xmal using DataGrid )

 WPF MVVM JSON Operations :(Part - I : Read Json File Data to List and Show on Xmal using DataGrid )

See the folder structure and Code as per the Folders and Design Pattern
 [
    {
       "firstName": "Joe",
       "lastName": "Jackson",
       "gender": "male",
       "age": 28,
       "number": "7349282382"
    },
    {
       "firstName": "James",
       "lastName": "Smith",
       "gender": "male",
       "age": 32,
       "number": "5678568567"
    },
    {
       "firstName": "Emily",
       "lastName": "Jones",
       "gender": "female",
       "age": 24,
       "number": "456754675"
    }
  ]




public class PersonModel:NotifyPropertyChangedHandler
    {
        private string firstname;

        public string FirstName
        {
            get { return firstname; }
            set { firstname = value; NotifyPropertyChanged("FirstName"); }
        }

        private string lastname;

        public string LastName
        {
            get { return lastname; }
            set { lastname = value; NotifyPropertyChanged("LastName"); }
        }

        private string gender;

        public string Gender
        {
            get { return gender; }
            set { gender = value; NotifyPropertyChanged("Gender"); }
        }

        private int age;

        public int Age
        {
            get { return age; }
            set { age = value; NotifyPropertyChanged("Age"); }
        }

        private double number;

        public double Number
        {
            get { return number; }
            set { number = value; NotifyPropertyChanged("Number"); }
        }

        public List<PersonModel> Items { get; set; }
    }



public class NotifyPropertyChangedHandler : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

 public class PersonViewModel
    {
        private PersonModel PersonModel;
        public List<PersonModel> Items { get; set; }

        public PersonViewModel()
        {
            PersonModel = new PersonModel();
            GetJsonData();
        }

        private void GetJsonData()
        {
            string json = System.IO.File.ReadAllText(@"PeopleData.json");
            Items = JsonConvert.DeserializeObject<List<PersonModel>>(json);
        }

    }
<Window x:Class="WpfJsonDataReader.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfJsonDataReader"
        xmlns:vm="clr-namespace:WpfJsonDataReader.ViewModel"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <vm:PersonViewModel x:Key="ViewModel"/>
    </Window.Resources>
    
    <Grid  DataContext="{Binding Source={StaticResource ViewModel}}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <DataGrid x:Name="UsersDataGrid" Grid.Row="0" AutoGenerateColumns="False" ItemsSource="{Binding Items}" 
                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                  ScrollViewer.VerticalScrollBarVisibility="Visible" CanUserAddRows="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="First Name" Width="100" Binding="{Binding FirstName}" />
                <DataGridTextColumn Header="Last Name" Width="100" Binding="{Binding LastName}" />
                <DataGridTextColumn Header="Gender" Width="100" Binding="{Binding Gender}" />
                <DataGridTextColumn Header="Age" Width="100" Binding="{Binding Age}" />
                <DataGridTextColumn Header="Number" Width="100" Binding="{Binding Number}" />

            </DataGrid.Columns>
            
        </DataGrid>
        
    </Grid>
</Window>





Comments

Popular posts from this blog

Simple WPF MVVM Application with Unit Testing.