Dazzler
Dazzler

Reputation: 382

Get selected Item name from combo box

I have a combo box as shown in the code below. I would like to display the name of the selection in a message box when I select it. What I am trying is -

<dxb:BarEditItem.EditTemplate>
<DataTemplate>
    <dxe:ComboBoxEdit x:Name="PART_Editor"
                      SelectedIndexChanged="OnSelectedIndexChanged" Name="comboBox">
        <dxe:ComboBoxEdit.Items>
            <system:String>Item1</system:String>
            <system:String>Item2</system:String>
        </dxe:ComboBoxEdit.Items>
    </dxe:ComboBoxEdit>
</DataTemplate>

How can I add the code in the backend for getting the selected name inside a message box?

Upvotes: 0

Views: 2053

Answers (3)

just type comboboxName.Text and you will get the selected item of combobox

Upvotes: 0

Grx70
Grx70

Reputation: 10339

I'm not sure what do you mean by the "name of the selection", so I'm assuming you want to get hold of the text that is displayed in the combo, which represents the selected item.

Once you have the combo itself in your hands:

private void OnSelectedIndexChanged(object sender, RoutedEventArgs e)
{
    var combo = (ComboBoxEdit)sender;
    (...)
}

you have several options. Most reliable one (in my opinion) would be to use combo.DisplayText property, which is a read-only property holding the actual text that should be displayed in the combo (with consideration of DisplayMember property, DisplayTextConverter property and CustomDisplayText event).

Another option (in your particular case) would be (string)combo.SelectedItem. Note though, that combo.SelectedItem returns the actual selected item and not it's text representation. The above is fine as long as items are of type string. Should they not be, you'll get an InvalidCastException. Also, it's possible that in that case what you get might not be what you see (as noted in previous paragraph there are several ways to modify the displayed text).

Yet another option is combo.Text, which takes into consideration DisplayMember, but not DisplayTextConverter nor CustomDisplayText.

EDIT

Turns out that at the time SelectedIndexChanged is raised, DisplayText property is not yet updated to reflect the newly selected item (which isn't especially surprising). To deal with that, you should "postpone" the retrieval of the DisplayText value. I'd personally go with something along these lines (using a Dispatcher associated with the combo):

private void OnSelectedIndexChanged(object sender, RoutedEventArgs e)
{
    var combo = (ComboBoxEdit)sender;
    combo.Dispatcher.BeginInvoke(new Action(() =>
    {
        var text = combo.DisplayText;
        (...)
    }));
}

Upvotes: 1

jegtugado
jegtugado

Reputation: 5141

Do you mean to handle this on the SelectedIndexChanged event? If so you can get the combobox that triggered the event.

private void OnSelectedIndexChanged(object sender, RoutedEventArgs e)
{
    ComboBox cb = (ComboBox)sender;
    string selectedText = cb.SelectedText;
    //Code to display the selectedText into a message box
}

Upvotes: 2

Related Questions