DataGrid.LoadingRow Event in silverlight


Introduction :DataGrid.LoadingRow Event. This event occurs after a DataGridRow is instantiated, so that you can customize it before it is used.

public event EventHandler<DataGridRowEventArgs> LoadingRow
<sdk:DataGrid LoadingRow="eventhandler"/>

Explanation : Loadingrow event occurs when rows loaded in datagrid.

we can identify the controls which added in the datagrid like

checkbox,buttons etc.with the help of the FrameworkElement cellContent.

We can get events of that controls in loadingrow event as below.

 void dataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
          
            DataGridRow row = e.Row;
            foreach (DataGridColumn col in dataGrid.Columns)
            {
              
                FrameworkElement cellContent = col.GetCellContent(e.Row);
                //Button b = cellContent as Button;
                CheckBox chk = cellContent as CheckBox;
                if (cb != null)
                {
                    chk.Checked -= cb_Checked;
                    chk.Checked += new RoutedEventHandler(cb_Checked);
                    chk.Unchecked += new RoutedEventHandler(cb_Unchecked);
                }
            }

        void chk_Checked(object sender, RoutedEventArgs e)
        {
            //Checkbox checked event
        }
        void chk_Unchecked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            if (CheckedRow.ContainsKey(chk))
            {
                CheckedRow.Remove(chk);
            }
        }

      void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            DataGridRow row = e.Row;
            foreach (DataGridColumn col in dataGrid1.Columns)
            {
                FrameworkElement cell = col.GetCellContent(e.Row);
                cell.MouseLeftButtonDown += new MouseButtonEventHandler(this.DataGridCell_OnMouseLeftButtonUp);
            }
        }  
        private void DataGridCell_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (DateTime.Now - this._lastClick <= DoubleClickThreshold)
            {
                TextBlock tb = (((System.Windows.Controls.Panel)(sender))).Children[0] as TextBlock;
                MessageBox.Show(tb.Text);
            }
            this._lastClick = DateTime.Now;
        }