Tags: combobox, content, display, dropdownlist, editable, key, microsoft, msdn, path, selectedvalue, software, studio, text, textbox, visual

Editable ComboBox and SelectedValue(Path)

On Microsoft » Microsoft Visual Studio

3,087 words with 2 Comments; publish: Mon, 07 Jan 2008 19:44:00 GMT; (30091.80, « »)


I have an editable ComboBox. But I want display in the TextBox another content than in the DropDownList. The ComboBox.Text should show a key and the DropDownList a string with key + text.

My items are of type ComboItem, which has two properties ckey and ctext. I've set SelectedValuePath to "ckey" and I override ToString() of ComboItem to return ckey + ctext. The DropDownList shows now the ckey+ctext.

I have defined an event for SelectionChanged of the ComboBox. In this event I set the ComboBox.Text to SelectedItem.ckey. This fires the same event again and the programm tries to find the new SelectedItem. This fails, because the programm compares the content of ComboBox.Text with all ComboItem.ToString() and finds no item and then my ComboBox is emtpy, though the ComboBox is editable.

I really expected, that the programm compares the value of the text with the SelectedValue (defined by the SelectedValuePath) and not with the ToString(). The ToString() shold only good to fill the DropDownList.

What's the SelectedValuePath really good ?



All Comments

Leave a comment...

    • Hello, since you’ve set SelectedValuePath to ckey, now the item in the data source whose ckey’s value equals to the SelectedValue you defined (if you define one) will be automatically selected. This is how you use SelectedValuePath.

      In your case, you can simply use:

      <ComboBox IsEditable=”True” TextSearch.TextPath=”ckey” …/>

      You don’t need to handle SelectionChanged event.

      #1; Tue, 02 Oct 2007 11:12:00 GMT
    • Hello, that works fine.

      When I know to search for in the documentation, than it's very easy.

      Thank You.

      The ComboBox consists of several controls.

      Can I set some properties of the underlaying TextBox (e.g. MaxLength or CharacterCasing or some other) ?

      Why are these properties not supported by ComboBox ?

      My editable ComboBox should sometimes be "readonly" to display a text without the user can input text or open the box. When I set ComboBox.Readonly = true and ComboBox.Text = "xxxx" that has peculiar effects, because the Items in the Box are only supported when it opens (that is the Items are not supported in my "readonly-mode").

      I've set ComboBox.Enabled = false, but this looks not very pretty. My TextBoxes on my window with "readonly = true" look good and have the desired behavior.



      #2; Tue, 02 Oct 2007 11:13:00 GMT