-5

I'm using WPF, and I have a form, on which there are many textboxes, buttons and whatnot.

For a specific reason I need that whole form to be disabled, which I managed with the code below -

public WPForm() //constructor
    {
        this.IsEnabled = false;            
    }

But, I have one button that sends me back to another form, which I need to be enabled (clickable), and what I have tried is simply setting it like this -

public WPForm() //constructor
    {
        this.IsEnabled = false;   
        btnBack.IsEnabled = true;         
    }

And this does basically nothing, so my question is, what else do I need to do in order to make it enabled?

aca
  • 1,071
  • 5
  • 15
  • 1
    `foreach (Control ctrl in this.Controls.Where(c => c.Name != "btnBack")) ctrl.IsEnabled = false;` to be honest, you should be using utilizing mvvm with binding. – Trevor Apr 14 '22 at 14:03
  • I don't have this.Controls option – aca Apr 14 '22 at 14:04
  • `wpf` see https://stackoverflow.com/questions/974598/find-all-controls-in-wpf-window-by-type – Trevor Apr 14 '22 at 14:08
  • WPF, as I put in the tags – aca Apr 14 '22 at 14:08
  • 1
    Naming your `Window` `Form` and saying `I have a form` doesn't give me anything useful, I would assume `WinForms`, not `WPF`. Anyways, I would go the MVVM path and use binding for this as I've mentioned already. – Trevor Apr 14 '22 at 14:10
  • Yeah, maybe check the tag section, as I've mentioned already. Anyway, question is edited. Thanks for advice about MVVM, but I'm sure I can work with this just fine. – aca Apr 14 '22 at 14:13
  • 1
    `maybe check the tag section` you have both, doesn't help. – Trevor Apr 14 '22 at 14:17
  • No I don't. There is no WinForms, nor ever was. Form is indeed part of WPF. – aca Apr 14 '22 at 14:19
  • 1
    `Form` isn't a part of the `WPF` framework(please read https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/windows-forms-controls-and-equivalent-wpf-controls?view=netframeworkdesktop-4.8), it's part of `WinForms`. Why did you change your class name to `WinForm`? – Trevor Apr 14 '22 at 14:23
  • Please take my advice I've given to help. Hope it all works out, good luck! – Trevor Apr 14 '22 at 14:25
  • My bad, I got my brain busted staring at a PC whole day. Will take into considering, but still searching for something "simpler". – aca Apr 14 '22 at 14:26
  • `but still searching for something "simpler"` please re-read my comments. It doesn't take much to do what I've recommended (mvvm/binding) and is preferred; start with it now rather than later. – Trevor Apr 14 '22 at 14:27

1 Answers1

-2

Just dont set the whole Form to disabled. Try to put a Grid or Stackpanel around the Controls that need to be disabled and disable them only. Maybe like so:

<Grid x:Name="myGrid">
    //Your Controls
<Grid/>
<Button />


public WinForm() //constructor
{
    myGrid.IsEnabled = false;         
}
Florian_Schaf
  • 47
  • 1
  • 6
  • 1
    Why even do this, instead of the constructor doing this, why not just set the property itself. What happens when some other conditions change and perhaps need to change the state of other controls in this grid? – Trevor Apr 14 '22 at 14:19