Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable all buttons

I have a method that disables all the butttons on my window. But i can't seem to get the type of Button to match it to the Resource collection

I'm using Expression Blend 3 with a c# code-behind

void DisableButtons()
    {
        for(int i = 0; i>= this.Resources.Count -1; i ++)
        {
            if (this.Resources[i].GetType() == typeof(Button))
            {
                Button btn = (Button)this.Resources[i];
                btn.IsEnabled = false;
            }
        }

    }

Update

Thanks for the answers! Ok The loop is working but my code is incorrect. this.Resources Does not seem to include my buttons! This could be an Blend thing?

So Yeah. I ended up doing it manually. Cause I'm hasty and there isn't a short easy solution. Thanks for all the input though!

like image 783
Josef Van Zyl Avatar asked Nov 27 '25 20:11

Josef Van Zyl


1 Answers

void DisableButtons()
{
    for(int i = 0; i < Resources.Count; i ++)
    {
        var btn = Resources[i] as Button;
        if(btn != null)
        {
            btn.IsEnabled = false;
        }
    }
}

Easy way to implement it is use foreach instruction with LINQ query, but this way need more resuources whan easy for.

void DisableButtons()
{
    foreach(var button in Resources.OfType<Button>())
    {
        button.IsEnabled = false;
    }
}
like image 106
Viacheslav Smityukh Avatar answered Nov 30 '25 11:11

Viacheslav Smityukh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!