Wpf: getting combobox value

I have a WPF ComboBox named cbFileSize. I am trying to get the selected value like this:

string tmp = cbFileSize.SelectedValue.ToString();
MessageBox.Show(tmp);

But tmp gets the value "System.Windows.Control.ComboBoxItem: 16".

What function should I use to get the value "16"?

+3
source share
2 answers

string tmp = (cbFileSize.SelectedValue as ComboBoxItem) .Content.ToString ();

or

string tmp = (cbFileSize.SelectedItem as ComboBoxItem) .Content.ToString ();

( ): , , . ComboBoxItems ComboBox, Item selectedValue ComboBox:

<ComboBox x:Name="comboBox">
    <ComboBoxItem>15</ComboBoxItem>
    <ComboBoxItem>16</ComboBoxItem>
    <ComboBoxItem>17</ComboBoxItem>
</ComboBox>

, - . ComboBoxItem , , ( ).

, , , , , ( ints), ComboBox ComboBox.

+4

/ Tag comboboxitem

<ComboBoxItem Content="This Value" Tag="This Value"/>

:

GetValue=ComboBoxName.SelectedItem.Tag.ToString()

" " "System.Windows.Controls.ComboBoxItem: "

0

Source: https://habr.com/ru/post/1740025/


All Articles