How can I best handle WPF radio buttons?

后端 未结 2 1708
借酒劲吻你
借酒劲吻你 2021-02-20 09:27

I\'ve got some RadioButtons in my XAML...


    

        
2条回答
  •  离开以前
    2021-02-20 09:53

    In order for commands to work you need to set up bindings in either your xaml or code behind. These command bindings must reference public static fields that have been previously declared.

    Then in your buttons Command attribute you will then need to also reference these same commands.

    
        
            
            
            
        
        
            One
            Two
            Three
        
    
    
    public partial class Window1 : Window
    {
        public static readonly RoutedCommand CommandOne = new RoutedCommand();
        public static readonly RoutedCommand CommandTwo = new RoutedCommand();
        public static readonly RoutedCommand CommandThree = new RoutedCommand();
    
        public Window1()
        {
            InitializeComponent();
        }
    
        private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (e.Command == CommandOne)
            {
                MessageBox.Show("CommandOne");
            }
            else if (e.Command == CommandTwo)
            {
                MessageBox.Show("CommandTwo");
            }
            else if (e.Command == CommandThree)
            {
                MessageBox.Show("CommandThree");
            }
        }
    }
    

提交回复
热议问题