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,769 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,621 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. Markus Freitag 3,786 Reputation points
    2020-08-12T07:25:49.797+00:00

    Hi Peter,
    MVVM - You know I use the ViewModel in my main project.
    I make some test project and in this case, easier is the solution from @DaisyTian-MSFT

    Your solution is for MVVM, right?
    Solution from @DaisyTian-MSFT is 'normal' as WinForms.

    So how I can fill this <ComboBoxItem Tag="23" ToolTip="Blocked">Blocked</ComboBoxItem> by code?
    In case of the list is large or read from a text file or xml file.

    MVVM is with binding the controls, right and also code behind, so not possible with the solution from @DaisyTian-MSFT
    You call CodeBehind, when I use no binding for controls, right?

    0 comments No comments

  2. Markus Freitag 3,786 Reputation points
    2020-08-14T06:15:44.92+00:00

    Thanks, both of you. I can only accept one answer.

    Both are helpful.

    0 comments No comments

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.