0

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!

ASh
  • 34,632
  • 9
  • 60
  • 82
L Hu
  • 9
  • 1
  • Don't use code-behind for that. Use a ViewModel Property and bind visibility to it. – Fildor Oct 23 '18 at 09:12
  • Here is an example for a button: https://stackoverflow.com/questions/7000819/binding-a-buttons-visibility-to-a-bool-value-in-viewmodel#7000922 . You should be able to replace the button with your controls. – Fildor Oct 23 '18 at 09:18
  • Do you want to hide/ show your buttons or just enable disable them? – MindSwipe Oct 23 '18 at 09:35
  • @MindSwipe As I read it, OP does not want to toggle the _buttons'_ visibility but the use the button to toggle visibility of another control. – Fildor Oct 23 '18 at 10:56
  • @Fildor I understand that, but OPs wording is kinda confusing, he's saying he wants to "enable the one or the other" does he mean show/ hide the buttons or enabling/ disabling them? – MindSwipe Oct 23 '18 at 11:28
  • _"... a Numpad and a keyboard. Both are user controls and they are shown in a Window."_ - I guess he's referring to those. @MindSwipe – Fildor Oct 23 '18 at 11:30
  • @Fildor I know which controls hes referring to, but is he referring to the `Visibility` Property or the `IsEnabled` Property? Is he trying to make those controls disappear or simply make them so the User can't click on them? – MindSwipe Oct 23 '18 at 11:36
  • @MindSwipe Ahhh, sorry! Now I got it. Yes, you are correct. Could be any of the two. L Hu: please clearify. – Fildor Oct 23 '18 at 11:39

1 Answers1

0

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.

MindSwipe
  • 7,193
  • 24
  • 47
  • Exactly because OP is new to WPF I find it important to show the "correct way" to do it: Commands, ViewModel, Bindings. – Fildor Oct 23 '18 at 10:57