ComboBox Caret Position

628 views
Skip to first unread message

Kent Boogaart

unread,
Feb 20, 2009, 6:23:37 AM2/20/09
to WPF Disciples
Hi all,

Maybe this has been discussed before but I cannot find anything on it.

The other day I simply wanted to set an editable ComboBox's Text
property and then have the caret to the immediate right of this text
so that the user can continue typing. The ComboBox is actually inside
a DataGrid and the particular behavior I was addressing was ensuring
that the user could just start typing when the cell had focus. It
should immediately switch to edit mode and place the text they typed
in the ComboBox. However, if I didn't move the caret any further text
they typed would overwrite the initial text.

If I were dealing with a TextBox, I'd simply set the CaretIndex
property. However, ComboBox does not seem to have any equivalent
property or method, which seems like a pretty big oversight to me.

Here's how I ended up solving this little dilemma (don't laugh):

if (textArgs != null)
{
//replace text if the user started editing the cell by typing text
string inputText = textArgs.Text;
comboBox.Text = inputText;
//put caret after input - there appears to be no API on ComboBox to
do this more cleanly. Insane.
Dispatcher.BeginInvoke(new ThreadStart(delegate
{
InputManager.Current.ProcessInput(new KeyEventArgs
(InputManager.Current.PrimaryKeyboardDevice,
PresentationSource.FromDependencyObject(comboBox), 0, Key.Right)
{ RoutedEvent = Control.KeyDownEvent });
}));
}

Am I missing something embarrassingly obvious?

Thanks,
Kent

Peter O'Hanlon

unread,
Feb 20, 2009, 6:29:40 AM2/20/09
to wpf-di...@googlegroups.com
I'd possibly get the textbox itself from the ComboBox and use that:
 
protected TextBox EditableTextBox
{
    get
    {
        return (base.GetTemplateChild("PART_EditableTextBox") as TextBox);
    }
}

I haven't tried it, but it seems as though it should work.
--
Peter O'Hanlon

ken...@internode.on.net

unread,
Feb 20, 2009, 8:25:39 AM2/20/09
to WPF Disciples
Right, but that feels just as dirty as my solution. Given that the
ComboBox is supposed to encapsulate this editable behavior, would you
not agree that there *should* be a property to control this? The
Winforms ComboBox at least has the Select method which can be used to
set the caret position...

Bill Kempf

unread,
Feb 20, 2009, 8:31:54 AM2/20/09
to wpf-di...@googlegroups.com
All of the edit controls need some polishing, IMHO.  Selection in each is different.  Non-existent in some, Start and End properties in others, and Selection in the rest.  The interface here should be consistent, but it's not.  This is another one MS should add to the list of todos for the next release.

Until it's fixed, however, I'd go with the templated part approach.
--
Quidquid latine dictum sit, altum sonatur.
- Whatever is said in Latin sounds profound.

War is peace. Freedom is slavery.  Bugs are features.

Peter O'Hanlon

unread,
Feb 20, 2009, 8:42:04 AM2/20/09
to wpf-di...@googlegroups.com
It does feel *dirty*, and I'm none too keen on having to do the hack, but (as Bill noted so well), the edit controls do differ quite wildly. I really wish there was a no-hack approach, but until this point I'll stick with this one.
--
Peter O'Hanlon

ken...@internode.on.net

unread,
Feb 20, 2009, 9:45:40 AM2/20/09
to WPF Disciples
Thanks guys.
Reply all
Reply to author
Forward
0 new messages