WPF C#

 WPF C# - Windows Presentation Foundation


Project 1 : WPF Create Bar | Chart | Graph | Line | Series 




npm>  install DataVisualization.Toolkit


MainWindow.xaml

<Window x:Class="WpfApp2.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:WpfApp2"
        mc:Ignorable="d"
        Title="" Height="640" Width="1080" AllowDrop="True" AllowsTransparency="True" Background="Transparent" WindowStyle="None">
    <Border BorderThickness="0" CornerRadius="50" >
        <Border.Background>
            <LinearGradientBrush StartPoint="0,1" EndPoint="1,0">
                <GradientStop Color="#FF31B5DF" Offset="0" />
                <GradientStop Color="MediumPurple" Offset="1" />
            </LinearGradientBrush>
        </Border.Background>

        <Grid Opacity=".7" Margin="10,0,0,25">
            <Label Content="Name" HorizontalAlignment="Left" Margin="32,51,0,0" VerticalAlignment="Top" FontFamily="Bahnschrift" Foreground="#FFF7F3F3" RenderTransformOrigin="0.5,0.5">
                <Label.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform/>
                        <SkewTransform AngleX="-1.682"/>
                        <RotateTransform/>
                        <TranslateTransform X="0.358"/>
                    </TransformGroup>
                </Label.RenderTransform>
            </Label>
            <TextBox x:Name="Name" HorizontalAlignment="Left" Margin="107,51,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="118">
                <AutomationProperties.LabeledBy>
                    <UIElement/>
                </AutomationProperties.LabeledBy>
            </TextBox>
            <Label Content="Designation" HorizontalAlignment="Left" Margin="32,80,0,0" VerticalAlignment="Top" FontFamily="Bahnschrift" Foreground="#FFF7F3F3"/>
            <TextBox x:Name="Mobile" HorizontalAlignment="Left" Margin="107,80,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="118">
                <AutomationProperties.LabeledBy>
                    <UIElement/>
                </AutomationProperties.LabeledBy>
            </TextBox>
            <Label Content="Email" HorizontalAlignment="Left" Margin="32,110,0,0" VerticalAlignment="Top" FontFamily="Bahnschrift" Foreground="#FFF7F3F3"/>
            <TextBox x:Name="Email"  HorizontalAlignment="Left" Margin="107,110,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="118">
                <AutomationProperties.LabeledBy>
                    <UIElement/>
                </AutomationProperties.LabeledBy>
            </TextBox>
            <Label Content="City" HorizontalAlignment="Left" Margin="32,140,0,0" VerticalAlignment="Top" FontFamily="Bahnschrift" Foreground="#FFF7F3F3"/>
            <TextBox x:Name="City"  HorizontalAlignment="Left" Margin="107,144,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="118">
                <AutomationProperties.LabeledBy>
                    <UIElement/>
                </AutomationProperties.LabeledBy>
            </TextBox>
            <Label Content="Experience" HorizontalAlignment="Left" Margin="32,175,0,0" VerticalAlignment="Top" FontFamily="Bahnschrift" Foreground="#FFF7F3F3"/>
            <TextBox x:Name="Postal" HorizontalAlignment="Left" Margin="107,179,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="118">

            </TextBox>
            <Button x:Name="btnSubmit" Content="Submit" HorizontalAlignment="Left" Margin="52,212,0,0" VerticalAlignment="Top" Height="30" Width="60" Click="btnSubmit_Click"/>
            <DataGrid x:Name="gv1" Margin="254,51,572,453"/>
            <Button Content="Next" HorizontalAlignment="Left" Margin="135,212,0,0" VerticalAlignment="Top" Height="30" Width="40" Click="Button_Click_2"/>

            <Button Content="New" HorizontalAlignment="Left" Margin="185,212,0,0" VerticalAlignment="Top" Height="30" Width="40" Click="Button_Click"/>
            <Button Content="X" VerticalAlignment="Top" Height="38" Click="Button_Click_1" Grid.ColumnSpan="1" Margin="32,14,988,0"/>

            <Grid Margin="32,281,30,-13">
                <Frame x:Name="MainFrame" Margin="10,5,10,5"/>
            </Grid>
            <TextBox x:Name="tsheet1" HorizontalAlignment="Left" Margin="308,187,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="27"/>
            <Label Content="Jan" HorizontalAlignment="Left" Margin="308,166,0,0" VerticalAlignment="Top"/>
            <TextBox x:Name="tsheet2" HorizontalAlignment="Left" Margin="338,187,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="27"/>
            <Label Content="Feb" HorizontalAlignment="Left" Margin="338,166,0,0" VerticalAlignment="Top"/>
            <TextBox x:Name="tsheet3" HorizontalAlignment="Left" Margin="373,187,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="27"/>
            <Label Content="Mar" HorizontalAlignment="Left" Margin="373,166,0,0" VerticalAlignment="Top"/>
            <TextBox x:Name="tsheet4" HorizontalAlignment="Left" Margin="403,187,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="27"/>
            <Label Content="Apr" HorizontalAlignment="Left" Margin="403,166,0,0" VerticalAlignment="Top"/>
            <TextBox x:Name="tsheet5" HorizontalAlignment="Left" Margin="436,187,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="27"/>
            <Label Content="May" HorizontalAlignment="Left" Margin="436,166,0,0" VerticalAlignment="Top"/>
            <TextBox x:Name="tsheet6" HorizontalAlignment="Left" Margin="466,187,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="27"/>
            <Label Content="Jun" HorizontalAlignment="Left" Margin="466,166,0,0" VerticalAlignment="Top"/>
            <TextBox x:Name="tsheet7" HorizontalAlignment="Left" Margin="308,226,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="27"/>
            <Label Content="Jul" HorizontalAlignment="Left" Margin="308,205,0,0" VerticalAlignment="Top"/>
            <TextBox x:Name="tsheet8" HorizontalAlignment="Left" Margin="338,226,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="27"/>
            <Label Content="Aug" HorizontalAlignment="Left" Margin="338,205,0,0" VerticalAlignment="Top"/>
            <TextBox x:Name="tsheet9" HorizontalAlignment="Left" Margin="373,226,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="27"/>
            <Label Content="Sep" HorizontalAlignment="Left" Margin="373,205,0,0" VerticalAlignment="Top"/>
            <TextBox x:Name="tsheet10" HorizontalAlignment="Left" Margin="403,226,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="27"/>
            <Label Content="Oct" HorizontalAlignment="Left" Margin="403,205,0,0" VerticalAlignment="Top"/>
            <TextBox x:Name="tsheet11" HorizontalAlignment="Left" Margin="436,226,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="27"/>
            <Label Content="Nov" HorizontalAlignment="Left" Margin="436,205,0,0" VerticalAlignment="Top"/>
            <TextBox x:Name="tsheet12" HorizontalAlignment="Left" Margin="466,226,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="27"/>
            <Label Content="Dec" HorizontalAlignment="Left" Margin="466,205,0,0" VerticalAlignment="Top"/>
            <Rectangle HorizontalAlignment="Left" Height="87" Margin="254,168,0,0" Stroke="LightGray" VerticalAlignment="Top" Width="246"/>
        </Grid>
    </Border>
</Window>



MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using WpfApp2.Model;

namespace WpfApp2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ObservableCollection<UserDetails> userDetailsList;
        public MainWindow()
        {
            InitializeComponent();
            userDetailsList = new ObservableCollection<UserDetails>();
        }

        

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {

            var dup=userDetailsList.Where(x => x.Email.ToLower() == Email.Text.ToLower());
            if (!dup.Any())
            {
                UserDetails userDetails = new UserDetails();
                userDetails.id = (userDetailsList.Count() + 1);
                userDetails.Name = Name.Text;
                userDetails.Designation = Mobile.Text;
                userDetails.Email = Email.Text;
                userDetails.City = City.Text;
                userDetails.Experience = Convert.ToInt32(Postal.Text);
                userDetails.Timesheets= addTimeSheet(userDetails);
                userDetailsList.Add(userDetails);

                gv1.ItemsSource = userDetailsList;
                gv1.Visibility = Visibility.Visible;
            }
            else
            {
                MessageBox.Show("User already exists.","",MessageBoxButton.OKCancel,MessageBoxImage.Error,MessageBoxResult.None,MessageBoxOptions.DefaultDesktopOnly);
            }
        }

        private List<TimeSheet> addTimeSheet(UserDetails userDetails)
        {
            List <TimeSheet> sheets = new List <TimeSheet>();

            for (int i = 1; i <= 12; i++)
            {
                TextBox textBox = (TextBox)FindName("tsheet" + i);
                if (textBox != null && int.TryParse(textBox.Text, out int value))
                {
                    // Add the parsed integer value to the list
                    sheets.Add(new TimeSheet { Months=i, DaysCompilance= value });
                }
                else
                {
                    MessageBox.Show($"Invalid input in TextBox{i}. Please enter a valid integer.");
                    //return; // Exit the method if input is invalid
                }
            }

            return sheets;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
           
            Window1 window1 = new Window1();
            // window1.Show(); // Win10 tablet in tablet mode, use this, when sub Window is closed, the main window will be covered by the Start menu.
            window1.ShowDialog();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            var secondPage = new Page1();
            secondPage.userDetails=userDetailsList;
            //secondPage.timeSheets = userDetailsList[0].Timesheets;

            secondPage.UpdateChart();

            //var secondPage = new Page2();   //toggle un/comments

            MainFrame.Navigate(secondPage);
        }
    }
}


UserDetails.cs

using System;
using System.Collections.Generic;
using System.Windows.Documents;


namespace WpfApp2.Model
{
    public class UserDetails
    {
        public UserDetails() {
          
        }
        
        public int id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Designation { get; set; }
        public int Experience { get; set; }
        public string City { get; set; }

        public List<TimeSheet> Timesheets { get; set; }

    }

    public class TimeSheet
    {
        public int Months { get; set; }
        public int DaysCompilance { get; set; }
    }
}



Page1.xaml

<Page x:Class="WpfApp2.Page1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:WpfApp2" 
      xmlns:dvc="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=DotNetProjects.DataVisualization.Toolkit"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="1080"
      Title="Page1">

    <Grid Margin="0,0,-38,0">
        <Grid Margin="0,0,400,0">
            <dvc:Chart Title="Bar-ColumnSeries Chart" Margin="0,0,209,0">
                <dvc:ColumnSeries IndependentValuePath="Name" DependentValuePath="Experience" 
                       ItemsSource="{Binding userDetails}" Title="Name" Margin="0,0,-4,0">
                </dvc:ColumnSeries>
            </dvc:Chart>
        </Grid>
        <Grid Margin="400,0,80,0">
            <dvc:Chart Name="chart" Title="LineSeries Chart" Margin="29,0,10,0">
                <dvc:LineSeries Margin="10,0,-67,0">
                </dvc:LineSeries>
            </dvc:Chart>
        </Grid>
    </Grid>
</Page>




Page1.xaml.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Controls.DataVisualization.Charting;
using WpfApp2.Model;


namespace WpfApp2
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class Page1 : Page
    {       
        public ObservableCollection<UserDetails> userDetails { get; set; }
        
        public Page1()
        {
            InitializeComponent();
            DataContext = this;

            userDetails = new ObservableCollection<UserDetails>();
            // Subscribe to the CollectionChanged event
            userDetails.CollectionChanged += UserDetails_CollectionChanged;
        }

        private void UserDetails_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            // Call your method here
            UpdateChart();
        }

        public void UpdateChart()
        {
            // Clear existing series
            chart.Series.Clear();

            // Iterate through each set of data lists
            for (int i = 0; i < userDetails.Count; i++)
            {
                List<TimeSheet> dataList = userDetails[i].Timesheets;

                // Create a new LineSeries to hold the data
                LineSeries lineSeries = new LineSeries();
                lineSeries.Title = userDetails[i].Name; //$"Series {i + 1}";

                // Bind the LineSeries to the data
                lineSeries.ItemsSource = dataList;

                // Set the DependentValuePath and IndependentValuePath
                lineSeries.DependentValuePath = "DaysCompilance";
                lineSeries.IndependentValuePath = "Months";

                // Add the LineSeries to the chart
                chart.Series.Add(lineSeries);
            }
        }
    }

   
}

Social Media