1

When I try this code:

foreach (Control ctrl in Grid1.Children)
            {
                    ctrl.Visibility = System.Windows.Visibility.Hidden;
             }

Or even this, which is what i'm really trying to do:

foreach (WebBrowser wb in Grid1.Children)
            {
                    wb.Visibility = System.Windows.Visibility.Hidden;
             }

and it gives me an exception : Unable to cast object of type 'System.Windows.Controls.WebBrowser' to type 'System.Windows.Controls.Control'. Any ideas?

  • It gives you that exception on the Visibility set? Seems like you would need a cast to get that exception. Also consider just binding the `Visibility` property instead of doing all this direct UI manipulation. – BradleyDotNET Oct 21 '14 at 20:53
  • 1
    Are you sure you want a grid of WebBrowsers? from [MSDN](http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser(v=vs.110).aspx)- `The WebBrowser control is resource-intensive. Be sure to call the Dispose() method when you are finished using the control to ensure that all resources are released in a timely fashion. You must call the Dispose() method on the same thread that attached the events, which should always be the message or user-interface (UI) thread.` If you have a bunch of them.... o.o (Just something to ponder*) – Nyra Oct 21 '14 at 20:54
  • I'm not sure i understand what that means. I am new to programming and C# and this was the best I could find so far, but it wouldn't work. Thanks for the reply though. – user1919646 Oct 21 '14 at 20:56
  • It means you should only use as many `WebBrowser` controls as you *need*. My comment was suggesting that you write WPF the way it was meant to be, with MVVM instead of coding directly to the UI. Do some googling, look around on SO, and if they still confuse you, ask a new question! – BradleyDotNET Oct 21 '14 at 20:58

2 Answers2

1

The short answer is to check to see if each control is a WebBrowser before you do anything with it:

foreach (var ctrl in Grid1.Children)
{
    if (ctrl is WebBrowser)
        ((WebBrowser)ctrl).Visibility = System.Windows.Visibility.Hidden;
}

However, you're trying to use WPF as if it were Windows Forms. It's not anything like Windows Forms, so you should accept that and try to do things the WPF way. The longer answer would be therefore be to use WPF properly and data bind some bool properties to the WebBrowser.Visibility properties using BooleanToVisibilityConverters. In that case, you'd be able to hide them all by setting a single property in your view model or code behind:

AreWebBrowsersVisible = false;

You'll be able to find plenty of examples of this online and even on this website. You'll find WPF a lot easier to use if you use it the WPF way.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Tried this and it gave me : Error 1 'object' does not contain a definition for 'Visibility' and no extension method 'Visibility' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) c:\users\PATH – user1919646 Oct 21 '14 at 20:58
  • Sorry, you'd need to cast first. I've updated the code. – Sheridan Oct 21 '14 at 21:02
  • 1
    As a place to start learning the "WPF way": http://stackoverflow.com/questions/1405739/mvvm-tutorial-from-start-to-finish – BradleyDotNET Oct 21 '14 at 21:03
0

What you are looking for is 'OfType':

    foreach (WebBrowser wb in grid1.Children.OfType<WebBrowser>())
        wb.Visibility = System.Windows.Visibility.Hidden;
pr0gg3r
  • 4,254
  • 1
  • 36
  • 27