1

I have ComboBox which I bind with list of some objects. ComboBox.SelectedItem and ComboBox.SelectedValue return the same object instance but I was thinking SelectedItem should return ComboBoxItem.

The problem is that I want to get selected text but the object is not string so .ToString() will not work.

Cephas PAD
  • 82
  • 9
Ashot Khachatryan
  • 2,156
  • 2
  • 14
  • 30
  • It's not really clear what you're asking here - surely you know what the item type is and how it's converted to its string representation? You should provide a [mcve] that demonstrates your problem and what you expect instead. – Charles Mager Oct 02 '16 at 08:12
  • @CharlesMager I clearly mentioned in question "I want to get selected text". If you know how to get selected text of combobox then please help if not then let others to answer. – Ashot Khachatryan Oct 02 '16 at 08:18
  • Actually the title of the question is not clear. Why can't you use ComboBox.Text to get selected text as String.? – Victor Sharovatov Oct 02 '16 at 08:21
  • @VictorSharovatov I tried to but it returns empty string "" I put that title because I don't understand the difference between SelectedItem and ComboBoxItem in my case they are the same – Ashot Khachatryan Oct 02 '16 at 08:22
  • Then you need to try using ComboBox.SelectedText. This post [link](http://stackoverflow.com/questions/10194171/combobox-selectedtext-doesnt-give-me-the-selectedtext) should help you. – Victor Sharovatov Oct 02 '16 at 08:28
  • @VictorSharovatov It doesn't have SelectedText property – Ashot Khachatryan Oct 02 '16 at 08:30
  • Sorry - I didn't notice WPF tag in your question. Please read [this post](http://stackoverflow.com/questions/3169328/how-to-get-combobox-selectedtext-in-wpf). Looks like it's about the same problem. – Victor Sharovatov Oct 02 '16 at 08:37

2 Answers2

1

ComboBox.SelectedItem returns an instance of the type of objects in the list, so you'll have to cast it to the appropriate type and then select the display property of that instance.

OR

It should be sufficient just to call Combox.Text, but it requires that SelectedItem != null and a defined DisplayMemberPath on the ComboBox.

If you want the Selected text in the open TextBox you can use reflection:

  var propInfo = typeof(ComboBox).GetProperty("EditableTextBoxSite", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
  var text = propInfo.GetValue(DataList) as TextBox;
  var selText = text.SelectedText;
  MessageBox.Show(selText);
1

You can bind SelectedItem to a property and set Selected value to that property when you make ComboBox SelectionChanged.

<ComboBox Name="cbxSalesPeriods"
                   Width="220" Height="30"
                   ItemsSource="{Binding SalesPeriods}"
                   SelectedItem="{Binding SelectedSalesPeriod}"
                   SelectionChanged="_ComboBoxCurrencyExchange_SelectionChanged">
        </ComboBox>

Here an ObservableCollectionnamed SalesPeriods containing SalesPeriodV object is bound as an ItemsSource of that ComboBox.

private ObservableCollection<SalesPeriodV> salesPeriods = new ObservableCollection<SalesPeriodV>();
public ObservableCollection<SalesPeriodV> SalesPeriods
{
    get { return salesPeriods; }
    set { salesPeriods = value; OnPropertyChanged("SalesPeriods"); }
}
private SalesPeriodV selectedItem = new SalesPeriodV();
public SalesPeriodV SelectedItem
{
    get { return selectedItem; }
    set { selectedItem = value; OnPropertyChanged("SelectedItem"); }
}

private void _ComboBoxCurrencyExchange_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ComboBox cb = (ComboBox)sender;
    SelectedItem = (SalesPeriodV)(cb.SelectedItem);
    string text = cb.SelectedValue.ToString();
}
J. Hasan
  • 492
  • 3
  • 14