Answer the question
In order to leave comments, you need to log in
Retrieving ComboBox Item in WPF?
Good afternoon! Faced a problem with extracting the ComboBox item. The markup is like this:
<ComboBox x:Name="CB_finWell" Margin="2 0 2 0" SelectionChanged="CB_finWell_SelectionChanged" >
<TextBlock><Run Text="ствол"/></TextBlock>
<TextBlock><Run Text="значение"/></TextBlock>
<TextBlock><Run Text="символ"/></TextBlock>
</ComboBox>
private void CB_finWell_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
ComboBoxItem selectedItem = (ComboBoxItem)comboBox.SelectedItem;
MessageBox.Show(selectedItem.Content.ToString());
}
TB_finWell.Text += ((TextBlock)CB_finWell.SelectedItem).Text;
Answer the question
In order to leave comments, you need to log in
MessageBox.Show($"{((ComboBoxItem)((ComboBox)sender).SelectedItem).Content}");
For TextBlock:
MessageBox.Show($"{((TextBlock)((ComboBox)sender).SelectedItem).Text}");
Updated:
If you need to pull the Run data from the TextBlock, then use Inlines.
MessageBox.Show($"{((Run)((TextBlock)((ComboBox)sender).SelectedItem).Inlines.ElementAt(0)).Text}");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question