ObservableCollection

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        private ObservableCollection<customer> custcol;
        public ObservableCollection<customer> custCol
        {
            get { return custcol; }
            set
            {
                custcol = value;
            }
        }
        public MainWindow()
        {
            InitializeComponent();
            custcol = new ObservableCollection<customer>();
            custCol.Add(new customer { custID = 1, custName = "1", Status = "InActive", Flag = true });
            custCol.Add(new customer { custID = 2, custName = "2", Status = "InActive", Flag = false });
            custCol.Add(new customer { custID = 3, custName = "3", Status = "InActive", Flag = false });
            datagrid1.ItemsSource = this.custCol;
        }
        private void HeadCheck(object sender, RoutedEventArgs e, bool IsChecked)
        {
            foreach (var  mf in this.custCol)
            {
                mf.Flag = true;
            }
           // dgMissingNames.Items.Refresh();
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            HeadCheck(sender, e, true);
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            HeadCheck(sender, e, false);
        }
        private void ChkAll_Checked(object sender, RoutedEventArgs e)
        {
            custcol = new ObservableCollection<customer>();
            custCol.Add(new customer { custID = 1, custName = "1", Status = "InActive", Flag = true });
            custCol.Add(new customer { custID = 2, custName = "2", Status = "InActive", Flag = false });
            custCol.Add(new customer { custID = 3, custName = "3", Status = "InActive", Flag = false });
            custCol.Add(new customer { custID = 3, custName = "3", Status = "InActive", Flag = false });
            custCol.Add(new customer { custID = 3, custName = "3", Status = "InActive", Flag = false });
            datagrid1.ItemsSource = this.custCol;
       
            datagrid1.Items.Refresh();
           var checkBox = sender as CheckBox;
            if (null == checkBox) return;
            foreach (var item in this.custCol)
                item.Flag = true;
        }

        private void ChkAll_Unchecked(object sender, RoutedEventArgs e)
        {

        }

        private void Chk_Checked(object sender, RoutedEventArgs e)
        {
            switch (((sender as CheckBox).Tag as customer).custID)
            {
                case 1: break;
                case 2: break;
                case 3: break;
            }
        }
    }
}
public class customer : INotifyPropertyChanged
{
    public object obj { get; set; }
    public int custID { get; set; }
    private string custname;
    public string custName
    {
        get { return custname; }
        set
        {
            custname = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("custName"));
            }
        }
    }
    public DateTime startTime { get; set; }
    public DateTime endTime { get; set; }

    private string status;
    public string Status
    {
        get { return status; }
        set
        {
            status = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Status"));
            }
        }
    }

    private string duration;
    public string Duration
    {
        get { return duration; }
        set
        {
            duration = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Duration"));
            }
        }
    }

    public bool Flag { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
}

-----------------------------------------

<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>

        <DataGrid x:Name="datagrid1" AutoGenerateColumns="True">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Select Value">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Name="Chk" Tag="{Binding}" Checked="Chk_Checked"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>

                    <DataGridTemplateColumn.HeaderTemplate>
                        <DataTemplate>
                            <CheckBox Name="ChkAll" Checked="ChkAll_Checked"  Unchecked="ChkAll_Unchecked"  IsThreeState="False" Padding="4,3,4,3" HorizontalContentAlignment="Center" HorizontalAlignment="Center"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.HeaderTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

          </Grid>


</Window>

Comments

Popular posts from this blog

25ThApril 2020