How to bind RadioButtons to an enum?

前端 未结 9 2085
日久生厌
日久生厌 2020-11-22 03:21

I\'ve got an enum like this:

public enum MyLovelyEnum
{
    FirstSelection,
    TheOtherSelection,
    YetAnotherOne
};

I got a property in

9条回答
  •  不思量自难忘°
    2020-11-22 04:10

    You can create the radio buttons dynamically, ListBox can help you do that, without converters, quite simple.

    The concrete steps are below:

    • create a ListBox and set the ItemsSource for the listbox as the enum MyLovelyEnum and binding the SelectedItem of the ListBox to the VeryLovelyEnum property.
    • then the Radio Buttons for each ListBoxItem will be created.
    • Step 1: add the enum to static resources for your Window, UserControl or Grid etc.
        
            
                
                    
                
            
        
    
    • Step 2: Use the List Box and Control Template to populate each item inside as Radio button
        
            
                
            
        
    

    The advantage is: if someday your enum class changes, you do not need to update the GUI (XAML file).

    References: https://brianlagunas.com/a-better-way-to-data-bind-enums-in-wpf/

提交回复
热议问题