Welcome to the Xceed Community Sign in | Join | Help
Community Search  

How to access the InsertionRow programatically?

Sort Posts: Previous Next
  •  05-11-2008, 10:09 PM Post no. 12155

    How to access the InsertionRow programatically?

    Hi,

    I am currently working on a project with ThoughtWorks, and we are trying (!) to do agile/Test Driven Development on a project using the Xceed Grid.

    We want to be able to access the insertion row programatically, in order to replicate (in a automated unit test) a user typing data into the InsertionRow.

    The problem is we can't seem to work out how to get a 'handle' to the InsertionRow.

    We are hoping once we have that we will be able to call Begin/EndEdit, and then start sending key strokes using Keyboard.FocusedElement.RaiseEvent()

     

    (In an ideal world we would use the  UI Automation API, but obviously the Xceed grid is not supporting that until Q3/4).

    Can anyone offer any tips here or approaches we can use?

    Thanks,
    Jack 

  •  05-12-2008, 8:14 AM Post no. 12164 in reply to 12155

    Re: How to access the InsertionRow programatically?

    This thread should point you in the right direction.

    Technical Writer - Xceed Software

    Of all the things I've lost, I miss my mind the most. - Mark Twain
  •  05-12-2008, 8:53 PM Post no. 12189 in reply to 12164

    Re: How to access the InsertionRow programatically?

    Hi Jenny,

    We are aware of the InitializingInsertionRow, unfortunately it does not appear to fire when the form loads (even though an insertion row appears to be rendered).

    The only way we can get the InitializingInsertionRow event to fire is to double click in a cell. Obviously this is of little use to us, as we need to run our tests without requiring any manual steps.

    Is there a way we can enter data into the grid (emulating a user), 100% programatically?

    Thanks,

    Jack 

  •  05-12-2008, 8:57 PM Post no. 12190 in reply to 12189

    Re: How to access the InsertionRow programatically?

    We have discovered one way to do this, but I feel its a bit of a hack:

    Using the VisualTreeHelper we are able to recursively descend the visual tree from the grid, until we find an object of type InsertionRow.

    As I said, it certainly feels a bit hacky.

    Would be much easier if the grid could simply expose the InsertionRow through a property or some such. That way we can just grab it whenever we want and start 'typing' in data.

    Regards,

    Jack
     

  •  05-13-2008, 8:30 AM Post no. 12206 in reply to 12190

    Re: How to access the InsertionRow programatically?

    If you want to get a reference to the InsertionRow in the headers of a grid, you will need to handle the InsertionRow's Loaded event and retrieve the reference there.
    Technical Writer - Xceed Software

    Of all the things I've lost, I miss my mind the most. - Mark Twain
  •  05-13-2008, 9:42 PM Post no. 12230 in reply to 12206

    Re: How to access the InsertionRow programatically?

    Ok, thanks that works.

    On the topic of attempting to automate/test the grid, can you give any advice as to how to go about this?

     
    We are finding complications with the STA/Dispatcher trying to get keystrokes sent to the cells.

    We are currently calling out test code from within DataGrid.Loaded event which takes the InsertionRow and basically iterates through the cells and attempts to 'type' in the contents by doing something like:

     
    cell.Focus()
    cell.BeginEdit()
    grid.Dispatcher.Invoke(DispatcherPriority.Input, (Callback)delegate
                {
                    foreach (char c in characters)
                    {
                        TextComposition textComposition = new TextComposition(InputManager.Current, Keyboard.FocusedElement, "my text");

                        TextCompositionEventArgs textCompositionEventArgs = new TextCompositionEventArgs(Keyboard.PrimaryDevice, textComposition);
                        textCompositionEventArgs.RoutedEvent = UIElement.TextInputEvent;
                        Keyboard.FocusedElement.RaiseEvent(textCompositionEventArgs);
                    }
                }
                );

    .. go to next cell... loop

     

    But the cells do not get populated.

    I suspect this is because the thread running through Loaded is the Dispatcher/UI thread, and there are problems with the keys being sent.

    A bit lost on this one. 

  •  05-14-2008, 1:09 AM Post no. 12234 in reply to 12155

    Re: How to access the InsertionRow programatically?

    Ok, I have finally cracked it.

    The key mistake I made was not calling UpdateLayout() after calling Cell.BeginEdit().

    Without calling UpdateLayout() the actual cell editor will not be materialised by the time we called SendKeys. (The layout process that creates/renders the cell editor presumably cannot fire until the current DispatcherOperation has finished, due to the way the Dispatcher cannot interrupt the current running operation). Without a cell editor available inside the InsertionCell's visual tree, there was no control interested in the keys  we were sending it.

                InsertionRow row = GetInsertionRow()

                row.BeginEdit();
                for (int i = 0; i < row.Cells.Count; i++)
                {
                    var cell = row.CellsIdea;

                    cell.BeginEdit();
                    // We must force a layout update or else the cell editor (TextBox)
                    // has not yet been created in the visual tree, by the time we call SendKeys
                    cell.UpdateLayout();
                    SendKeys( "your text here ");
                    cell.EndEdit();
                }
                row.EndEdit();
     

    SendKeys is implemented just like the example on the WPF wiki.  

View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2008 Xceed Software Inc.