简体   繁体   中英

Enable/disable ui element usercontrol in wpf c# code behind

For a project, I create a Numpad and a keyboard. Both are user controls and they are shown in a Window.

I want to use a button/toggle in xaml to enable the one or the other. What is the best way to approach this in code- behind C#? Thank you in advance!

Make a Button on the GUI and bind the Click event to a EventHandler in the Code Behind. This EventHandler basically just flip-flops the IsEnabled Property of the control Elements. Here is how something like this could look

private void BtnToggle_Click(object sender, RoutedEventArgs e)
{
    //Do this with every button
    BtnToFlip.IsEnabled = !Btn.ToFlip.IsEnabled
}

This flip-flops between IsEnabled from true to false

Note: This is in no way shape or form the best way to do this, and I am aware this is not even remotely MVVM. This is just a simple answer to OP's question as he seems to be very new.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM