WPF combobox value and display text

后端 未结 4 1035
無奈伤痛
無奈伤痛 2021-02-12 02:30

I\'m used to doing things like

State.Items.Add(new ListItem { Text = \"SomeState\", Value = NumericIDofState });

Where State is a Listbox in A

4条回答
  •  隐瞒了意图╮
    2021-02-12 03:07

    If you only want to expose a simple property in the viewmodel and handle the text for the choices in the view you can do a simple solution like this:

        
            
            
            
        
    

    Example with a bool property:

        
            
            
        
    

    Type-verbose alternatives (original examples)

    Below are more verbose alternatives where the types are explicitly declared. Depending on your preferred style (or maybe some types that requires it), maybe it suits you better.

    
        
            
                0
            
        
        
            
                1
            
        
        
            
                2
            
        
    
    

    Example with a bool property:

    
        
            
                False
            
        
        
            
                True
            
        
    
    

    The sys namespace is declared as this:

    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    

提交回复
热议问题