3

I am adding a Custom DataGridTextColumn that will allow me to fire an event when when ever the content is changed in this cell.

Please note:

I do not want to use a DataGridTemplateColumn with this as I know that. I want to create my own text column as there are a lot of features that come with text column that we use.

So I decided to just simply add an event to a custom control - simple enough. not so much. well it seams that there isn't an AddHandler or RemoveHandler methods.

Please explain where I am going wrong.

Code:

public static readonly RoutedEvent TextChangedEvent =
EventManager.RegisterRoutedEvent("TextChanged", RoutingStrategy.Bubble,
    typeof (RoutedEventHandler),
    typeof (DataGridTextChangedEventColumn));

public event RoutedEventHandler TextChanged
{
    add { AddHandler(TextChangedEvent, value); }
    remove { RemoveHandler(TextChangedEvent, value); }
}

private void AddHandler(RoutedEvent textChangedEvent, RoutedEventHandler value)
{
    this.TextChanged += (s, e) => textChangedEvent;
}

Thank you.

ViVi
  • 4,339
  • 8
  • 29
  • 52
JamTay317
  • 1,017
  • 3
  • 18
  • 37

1 Answers1

1

If you want to create "Your" customized DatagridTextColumn, you could create a CustomControl that inherits from DataGridTextColumn.

Doing this, you can override the method "GenerateEditingElement" that returns the control that is associated with the editing look of the grid (generally it is a TextBox).

While you are overriding this method, you can attach an event handler to Your TextChanged event.

public class YourCustomDataGridTextColumn : DataGridTextColumn
{


public delegate void ColumnTextChangedHandler(object sender,TextChangedEventArgs e);
public event ColumnTextChangedHandler ColumnTextChanged;

    #region "Methods"

    protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
    {
        var textBox = (TextBox)base.GenerateEditingElement(cell, dataItem);
        textBox.TextChanged += OnTextChanged;

        return textBox;
    }

    private void OnTextChanged(object sender, TextChangedEventArgs e)
    {
        //Your event handling
        if (ColumnTextChanged != null) {
                ColumnTextChanged(sender, e);
          }
    }

    #endregion    
}
Babbillumpa
  • 1,854
  • 1
  • 16
  • 21
  • thank you, the only thing is when I type 'GetBinding' from 'GenerateElement' it does not appear to be there. am i missing a mapping? – JamTay317 Jul 19 '16 at 10:39
  • @JamTay317 My error. I copied this snippet from a class i wrote and i missed deleting that method. If you want you can omit it because the default behaviour for DataGridTextColumn is to return a Textblock when you are in view mode. – Babbillumpa Jul 19 '16 at 10:59
  • how do you access from MainWindow? – JamTay317 Jul 19 '16 at 11:48
  • What do you mean? What do you have to do from MainWindow? – Babbillumpa Jul 19 '16 at 12:02
  • I don't understand. What do you want to do? – Babbillumpa Jul 19 '16 at 12:18
  • Okay, sorry for confusion, I have added your code along with public event TextChanged and now the OnTextChanged fires in the TextChangedEventTextColumnonly. it doen't fire TextChangedEventTextColumn_OnTextChanged in main window. – JamTay317 Jul 19 '16 at 12:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117707/discussion-between-cicciorocca-and-jamtay317). – Babbillumpa Jul 19 '16 at 12:33