Asim Sajjad
Asim Sajjad

Reputation: 2534

Selected Text of The ComboBox

how can I get the selected text on the SelectionChanged event of the comboBox here is my code

<ComboBox x:Name="cboRecordType" Margin="2,0" Height="23" Grid.Column="1" VerticalAlignment="Center" SelectionChanged="ComboBox_SelectionChanged">
            <ComboBoxItem Content="Weight"/>
            <ComboBoxItem Content="Height"/>
            <ComboBoxItem Content="Blood Pressure"/>
            <ComboBoxItem Content="Blood Gulocose"/>
        </ComboBox>

cboRecordType.Text is empty, didn't cantain the selected Text, how to get that

Upvotes: 1

Views: 566

Answers (4)

Levelbit
Levelbit

Reputation: 170

Better try using Command and CommandParametar as part of MVVM implementation.

Upvotes: 0

Amsakanna
Amsakanna

Reputation: 12954

Rather than handling events, you can try the binding approach. For that you need to create a property like this and bind it to your combobox's selected item

private String _selectedItem;
public String SelectedItem
{
    get { return _selectedItem; }
    set
    {
        _selectedItem = value;
        OnPropertyChanged(new PropertyChangedEventArgs("SelectedItem"));
    }
}

<ComboBox SelectedItem="{Binding SelectedItem}" />

SideNote: You can also fill in some collection and bind it to the combobox instead of hardcoding

Upvotes: 0

dthorpe
dthorpe

Reputation: 36082

In the SelectionChanged event handler, you can either look at the cboRecordType.SelectedItem property on the combobox itself, or you can look at the AddedItems property of the SelectionChangedEventArgs passed into the event handler.

When an item is selected, the item is added to the AddedItems array property of the event args. (multiple items in a multi select case). When an item is deselected, it is added to the RemovedItems array property of the event args.

Upvotes: 1

vodkhang
vodkhang

Reputation: 18741

In your code behind, you need to handle that event like this code: ComboBox SelectionChanged Code Block

/// <summary>

/// Handles the comboBox SelectionChanged event

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)

{



}

Upvotes: 0

Related Questions