title: WPF Datagrid: Programmatically editing a cell
body: <p>I have a cell that needs its value to be set on it just being clicked. It is multibound to different properties. </p>
  
  <p>Where am I supposed to do this? I have been trying to do it in the datagrid beginingedit handler like this (without much success). I am able to manually click twice(once to select cell and then to start edit) and the value gets set. But I want to do this programmatically...</p>
  
  <pre><code>private void MyDataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
  {
  
  TextBlock t = e.EditingEventArgs.OriginalSource as TextBlock;
  if (t == null) return;
  t.Text = SimulatedEdit();
  
  // All this below is just me trying different thing. Not sure what I need to be doing
  e.EditingEventArgs.Handled = true;
  MyDataGrid.CommitEdit();
  MyDataGrid.UnselectAllCells();
  }
  </code></pre>
  
  <p>This is how the columntemplate is setup</p>
  
  <pre><code>MultiBinding tempmb = new MultiBinding();
  Binding tempXB = new Binding("X");
  Binding temptYB = new Binding("Y");
  tempmb.Bindings.Add(tempXB);
  tempmb.Bindings.Add(temptYB);
  tempmb.ConverterParameter = "ggrid";
  tempmb.Converter = new LabelDecider();
  
              DataGridTemplateColumn dgtc = new DataGridTemplateColumn
              {
                  Header = "blah",  CanUserSort = false, CanUserReorder = false,
              };
              DataTemplate t = new DataTemplate();
              FrameworkElementFactory f = new FrameworkElementFactory(typeof(TextBlock));
              f.SetBinding(TextBlock.TextProperty, tempmb);
  
              // Setup background color binding
              MultiBinding colorb = new MultiBinding();
              colorb.Bindings.Add(tempX);
              colorb.Bindings.Add(tempY);
              colorb.ConverterParameter = "color";
              colorb.Converter = new LabelDecider();
              f.SetBinding(TextBlock.BackgroundProperty, colorb);
              t.VisualTree = f;
              //Every columns Text and Background are using bindings
              dgtc.CellTemplate = t;
  
              //setup editing template
              DataTemplate ced = new DataTemplate();
              FrameworkElementFactory f2 = new FrameworkElementFactory(typeof(TextBox));
              MultiBinding tempmb2 = new MultiBinding();
              tempmb2.Bindings.Add(tempXB);
              tempmb2.Bindings.Add(tempYB);
              tempmb2.Mode = BindingMode.TwoWay;
              tempmb2.ConverterParameter = "ggrid";
              tempmb2.Converter = new LabelDecider(rDestination.Recievers[k]);
  
              tempmb2.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
              f2.SetBinding(TextBox.TextProperty, tempmb2);
              ced.VisualTree = f2;
              dgtc.CellEditingTemplate = ced;
  
              MyDataGrid.Columns.Add(dgtc);
  </code></pre>