What Are xmlns and xmlns:x in WPF? Understanding XML Namespace Declarations

In WPF (Windows Presentation Foundation), xmlns and xmlns:x are XML namespace declarations used in XAML files. They define the scope of XML namespaces, which are used to distinguish between elements and attributes that might have the same name but are used in different contexts. xmlns : The Default XML Namespace Definition : xmlns stands for XML namespace. It is used to declare the default namespace for the elements in the XAML file. Purpose : In WPF, it typically maps to the .NET namespaces that contain the classes and controls used in WPF applications. This default namespace allows you to use the WPF controls and elements without prefixing them. Example : <Window x:Class="WpfApp.MainWindow"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    ... > <Grid>        <Button Content="Click Me"/>     </Grid> </Window> Here, xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" sets...

How to save data from datagrid to database in C# WPF?

DataGridEx.xaml

<Window x:Class="PrismApp.Views.DataGridEx"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutoWireViewModel="True">
    <Grid>
        <DataGrid ItemsSource="{Binding LstEmployees}"
                  AutoGenerateColumns="False" CanUserAddRows="False" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Employee Id" Binding="{Binding EmpId}" IsReadOnly="True"/>
                <DataGridTextColumn Header="Employee Name" Binding="{Binding EmpName,UpdateSourceTrigger=PropertyChanged}" />
                <DataGridTextColumn Header="Job Title" Binding="{Binding JobTitle,UpdateSourceTrigger=PropertyChanged}"/>
                <DataGridTextColumn Header="Experience" Binding="{Binding Experience,UpdateSourceTrigger=PropertyChanged}"/>
                <DataGridTextColumn Header="Salary" Binding="{Binding Salary,UpdateSourceTrigger=PropertyChanged}"/>
                <DataGridTemplateColumn Header="Update" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="P" FontFamily="Wingdings 2" FontWeight="ExtraBold"
                                    Background="Transparent" BorderThickness="0" FontSize="30"
                                    Command="{Binding DataContext.UpdateCommand,RelativeSource={RelativeSource AncestorType=DataGrid}}"
                                    CommandParameter="{Binding}">
                                <Button.Style>
                                    <Style BasedOn="{StaticResource {x:Type Button}}"  TargetType="Button">
                                        <Setter Property="Foreground" Value="Gray"/>
                                        <Setter Property="IsEnabled" Value="False"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsValueChanged,UpdateSourceTrigger=PropertyChanged}" Value="true">
                                                <Setter Property="Foreground" Value="Green"/>
                                                <Setter Property="IsEnabled" Value="True"/>

                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Button.Style>
                            </Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Window>
DataGridEx.xaml.cs

 public partial class DataGridEx : Window
    {
        public DataGridEx()
        {
            InitializeComponent();
        }
  }


DataGridExViewModel.cs

 public class DataGridExViewModel : BindableBase
    {
        public DataGridExViewModel()
        {

            LoadEmployees();
            LstEmployees.Select(x =>
            {
                x.IsValueChanged = false;
                return x;
            }).ToList();
        }

        private ObservableCollection<Employees> _lstEmployees = new ObservableCollection<Employees>();
        public ObservableCollection<Employees> LstEmployees
        {
            get { return _lstEmployees; }
            set { SetProperty(ref _lstEmployees, value); }
        }
        private DelegateCommand<Employees> _updateCommand;
        public DelegateCommand<Employees> UpdateCommand =>
            _updateCommand ?? (_updateCommand = new DelegateCommand<Employees>(Update));

        void Update(Employees employee)
        {
            using(var sqlite = new SqliteOperation(@"\Projects\emp.db"))
            {
                sqlite.Update(employee);
            }
            LstEmployees.Select(x =>
            {
                if(x.EmpId == employee.EmpId)
                    x.IsValueChanged = false;
                return x;
            }).ToList();
        }
        private void LoadEmployees()
        {
            using(var sqlite = new SqliteOperation(@"\Projects\emp.db"))
            {
                LstEmployees.AddRange(sqlite.Get());
            }
        }


    }

SqliteOperation.cs

 public class SqliteOperation : IDisposable
    {
        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
        private readonly SQLiteConnection connection;
        public SqliteOperation(string connectionString)
        {
            connection = new SQLiteConnection(connectionString);
            connection.CreateTable<Employees>();
        }
        public void Add(Employees employee)
        {
            connection.Insert(employee);
        }
        public void Update(Employees employee)
        {
            connection.Update(employee);
        }
        public List<Employees> Get()
        {
            return connection.Table<Employees>().ToList();
        }
    }

Employees.cs

 public class Employees : BindableBase
    {
        private int _empId;

        [AutoIncrement, PrimaryKey]
        public int EmpId
        {
            get { return _empId; }
            set { SetProperty(ref _empId, value); }
        }
        private string _empName;
        [NotNull]
        public string EmpName
        {
            get { return _empName; }
            set
           {
                if(_empName != value)
                    IsValueChanged = true;
                SetProperty(ref _empName, value);
            }
        }
        private string _jobTitle;
        [NotNull]
        public string JobTitle
        {
            get { return _jobTitle; }
            set
            {
                if(_jobTitle != value)
                    IsValueChanged = true;
                SetProperty(ref _jobTitle, value);
            }
        }
        private string _experience;
        [NotNull]
        public string Experience
        {
            get { return _experience; }
            set
            {
                if(_experience != value)
                    IsValueChanged = true;
                SetProperty(ref _experience, value);
            }
        }
        private double _salary;
        [NotNull]
        public double Salary
        {
            get { return _salary; }
            set
            {
                if(_salary != value)
                    IsValueChanged = true;
                SetProperty(ref _salary, value);
            }
        }
        private bool _isValueChanged;
        [Ignore]
        public bool IsValueChanged
        {
            get { return _isValueChanged; }
            set { SetProperty(ref _isValueChanged, value); }
        }
    }


Comments

Popular posts from this blog

Filter DataGrid and ListView in wpf using ICollectionView

Pagination of DataGrid in WPF using MVVM

How to Create TabControl using Prism Region