Force Uppercase for Cell Editing

3 posts, 1 answers
  1. Aaron
    Aaron avatar
    30 posts
    Member since:
    May 2011

    Posted 10 Aug 2012 Link to this post

    Hi,

    I have a RadGridView set up in a way that's similar to the 'search as you type' demo, and my View-Model is handling things like the auto-generation of the columns in the grid.  What I'd like to do is this.  When a row is created or edited, any text that is entered into a particular column needs to be in all caps.  How can I do it?



    Thanks,
    Aaron
  2. Answer
    Maya
    Admin
    Maya avatar
    4062 posts

    Posted 13 Aug 2012 Link to this post

    Hello Aaron,

    There are a couple of way to handle such case, but all of them require some custom implementation since TextBox (which is the default editing element does not support upper case out-of-the-box). What you can try is to handle PreparedCellForEdit event and find the TextBox:

    void playersGrid_PreparedCellForEdit(object sender, GridViewPreparingCellForEditEventArgs e)
            {
                var edtingElement = e.EditingElement as TextBox;
                if (edtingElement != null)
                {              
                }
         }
     
    Now you can work directly with the TextBox and implement the solution that sound most reasonable for you. Different approaches are suggested in those threads (link 1, link 2, link 3, link 4).

    Kind regards,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. Aaron
    Aaron avatar
    30 posts
    Member since:
    May 2011

    Posted 14 Aug 2012 Link to this post

    Thank you, Maya.

    In case anyone is interested, this is the solution I went with.

    1.    I created a TriggerAction class, strongly typed to TextBox, that calls the ToUpper() method on the Text property.

    public class UppercaseAction : TriggerAction<TextBox>
        {
            protected override void Invoke(object parameter)
            {
                var selectionStart = AssociatedObject.SelectionStart;
                var selectionLength = AssociatedObject.SelectionLength;
                AssociatedObject.Text = AssociatedObject.Text.ToUpper();
                AssociatedObject.SelectionStart = selectionStart;
                AssociatedObject.SelectionLength = selectionLength;
            }
        }

    2.    In my View-Model, I created an event handler for the RadGridView's PreparedCellForEdit event.

    public void OnPreparedCellForEdit(object sender, GridViewPreparingCellForEditEventArgs e)
            {
                // Unless the cell is in the Code or Cause Code column, return:
                if (e.Column.UniqueName != CODE_COLUMN_UNIQUE_NAME &&
                    e.Column.UniqueName != CAUSE_CODE_COLUMN_UNIQUE_NAME)
                {
                    return;
                }
     
                var editingElement = e.EditingElement as TextBox;
                if (editingElement != null)
                {
                    // Create an event trigger for a TextChanged event:
                    System.Windows.Interactivity.EventTrigger eventTrigger = new System.Windows.Interactivity.EventTrigger("TextChanged");
     
                    // Add an UppercaseAction to the event trigger:
                    eventTrigger.Actions.Add(new UppercaseAction());
     
                    // Add the event trigger to the cell's TextBox:
                    Interaction.GetTriggers(editingElement).Add(eventTrigger);
                }
            }

    3.    In the View, I bound the View-Model's event handler to the View using a CallMethodAction.

    <telerik:RadGridView x:Name="radGridView1">
        <iny:Interaction.Triggers>
            <iny:EventTrigger EventName="PreparedCellForEdit">
                <ins:CallMethodAction MethodName="OnPreparedCellForEdit" TargetObject="{Binding}"/>
            </iny:EventTrigger>
        </iny:Interaction.Triggers>           
    </telerik:RadGridView>

    The XML namespaces:

    The link I used was this one (number 3).
Back to Top