How to Enable/Disable button in wpf

前端 未结 5 384
温柔的废话
温柔的废话 2021-01-02 10:51

I Have 2 button , Start And capture. I want disable capture button on form load and enable start. and on after click on start disable start button and enable capture. Pleas

5条回答
  •  遥遥无期
    2021-01-02 11:17

    You should store current state in some variable (e.g. _capturing). When variable changing, you refresh IsEnabled property.

    Xaml code:

    C# code:

    public partial class MainWindow : Window
    {
        private bool _capturing;
    
        public MainWindow()
        {
            InitializeComponent();
    
            // Some code
    
            _capturing = false;
            UpdateButtons();
        }
    
        private void StartButton_Click(object sender, RoutedEventArgs e)
        {
            // Some code
    
            _capturing = true;
            UpdateButtons();
        }
    
        private void CaptureButton_Click(object sender, RoutedEventArgs e)
        {
            // Some code
    
            UpdateButtons();
        }
    
        private void UpdateButtons()
        {
            StartButton.IsEnabled = !_capturing;
            CaptureButton.IsEnabled = _capturing;
        }
    }
    

    UPDATE

    You should add click handler and your xaml code will work:

    
    
    

提交回复
热议问题