WPF, ComboBox, Tooltip or text, return value should be int or enum

Markus Freitag 3,786 Reputation points
2020-08-11T08:20:07.04+00:00

Hello,
<ComboBox Name="cbStatus" HorizontalAlignment="Left" Margin="660,13,0,0" VerticalAlignment="Top" Width="48" SelectionChanged="CbStatus_SelectionChanged">
<ComboBoxItem>20</ComboBoxItem>
<ComboBoxItem>21</ComboBoxItem>
<ComboBoxItem>22</ComboBoxItem>
<ComboBoxItem>23</ComboBoxItem>
<ComboBoxItem IsSelected="True">24</ComboBoxItem>
</ComboBox>
How can I insert a tooltip?

How can I display the text, but the selected value is a number?

string cb_State = ((ComboBoxItem)cbStatus.SelectedItem).Content.ToString();

Only for this control.
20 = "Automatic"
21 = "Error"
22 = "Wait"
23 = "Blocked"
24 = "Rmove"

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,626 Reputation points
    2020-08-14T02:55:22.647+00:00

    I will give you a sample which use MVVM to bind the data for ComboBox.

    public class Model  
        {  
            private string cmbValue;  
            public string CmbValue  
            {  
                get { return cmbValue; }  
                set { cmbValue = value; }  
            }  
            private string cmbText;  
            public string CmbText  
            {  
                get { return cmbText; }  
                set { cmbText = value;  }  
            }      
        }  
    

    The View model is:
    17559-capture.png

    The MainWindow.xaml is :

    <ComboBox Name="cbStatus"  
                      ItemsSource="{Binding Comlist}"   
                      SelectedValuePath="CmbValue"       
                      DisplayMemberPath="CmbText"     
                      Width="100" Height="30"  
                      SelectedItem="{Binding Comlist[4]}"  
                      SelectionChanged="cbStatus_SelectionChanged"  
                      />  
    

    Then you can get the value or text by:

     private void cbStatus_SelectionChanged(object sender, SelectionChangedEventArgs e)  
            {  
                string cb_StateValue = ((Model)cbStatus.SelectedItem).CmbValue;  
                string cb_StateText = ((Model)cbStatus.SelectedItem).CmbText;  
            }  
    
    1 person found this answer helpful.
    0 comments No comments

6 additional answers

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,326 Reputation points
    2020-08-11T16:51:48.773+00:00

    Hi Markus,
    thes best way is to use Extension like this:

      <Window.DataContext>  
        <local:ViewModel/>  
      </Window.DataContext>  
      <StackPanel>  
        <ComboBox ItemsSource="{Binding Source={local:EnumBindingSource {x:Type local:StatusEnum}}}"  
                  SelectedItem="{Binding SelectedEnumValue}"/>  
        <Label Content="{Binding StatusInfo}"/>  
      </StackPanel>  
    

    And code:

    16970-x.png
    17024-11-08-2020-18-54-37.gif

    1 person found this answer helpful.

  2. Peter Fleischer (former MVP) 19,326 Reputation points
    2020-08-12T08:40:27.953+00:00

    Hi Markus,
    My demo uses MVVM patterns with no code-behind. This is easier to control, especially when you are getting a large amount of data from a text file or an XML file. In the subject you asked for enum. Enums cannot simply be generated from text files or XML files at runtime.

    Binding and CodeBehind are independent things. You can also declare properties in the CodeBehind , which are then used for binding in XAML.

    If you get large amounts of data from text files or XML files, the easiest way is to fill a list of your own data objects with this data and to bind this list to ItemsSource. As SelectedItem you get the data object and you can read out both text and number.

    1 person found this answer helpful.

  3. DaisyTian-1203 11,626 Reputation points
    2020-08-12T01:56:46.527+00:00

    You can use Tag and ToolTip property for the ComboBoxItem to implement it for the only one control.

          <ComboBox Name="cbStatus" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" SelectionChanged="CbStatus_SelectionChanged">
                    <ComboBoxItem Tag="20"  ToolTip="Automatic">Automatic</ComboBoxItem>
                    <ComboBoxItem Tag="21" ToolTip="Error">Error</ComboBoxItem>
                    <ComboBoxItem Tag="22" ToolTip="Wait">Wait</ComboBoxItem>
                    <ComboBoxItem Tag="23" ToolTip="Blocked">Blocked</ComboBoxItem>
                    <ComboBoxItem Tag="24" ToolTip="Remove" IsSelected="True">Remove</ComboBoxItem>
                </ComboBox>
    

    Get the number in the cs by :

     string cb_State = ((ComboBoxItem)cbStatus.SelectedItem).Tag.ToString();
    
    0 comments No comments

  4. Markus Freitag 3,786 Reputation points
    2020-08-12T06:59:34.523+00:00

    @DaisyTian-MSFT,
    Looks good and easy. Easier as the solution from anonymous user-3316

    I think Peter use choose the complicated method because of the binding.

    Can we connect the two?
    How could I fill it with CodeBehind? (Tag, ToolTip)

    I've tested that. Do you have another good idea? See the files17059-mainwindow-kopie.txt17108-mainwindowxaml-kopie-cs.txt17109-viewmodel-kopie-cs.txt

    Thank you.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.