0

I have following code -

WebBrowser bw = new WebBrowser();
Grid gr = new Grid();
gr = ((ClosableTab)tabControl1.Items[tabControl1.SelectedIndex]).Content as Grid;
Grid grc = new Grid();
grc = gr.Children[1] as Grid;
bw = grc.Children[0] as WebBrowser;
bw.Source = new Uri(txtBoxUrl.Text);

Howvever I am able to fulfill my requirement, but all the elements are hardcoded. I know there is a better way and a single line of code. Please suggest a standard code to do this.

Update

<TabControl x:Name="tabControl1" FontWeight="UltraBlack" BorderThickness="1" BorderBrush="Black" Background="LightBlue" >
            <local:ClosableTab Background="LightBlue" Title="Preview" x:Name="PreviewWindow">
                <Grid>
                    <Border Name="mask" Background="Black" CornerRadius="5"/>
                    <Grid>
                        <Grid.OpacityMask>
                            <VisualBrush Visual="{Binding ElementName=mask}"/>
                        </Grid.OpacityMask>
                        <WebBrowser Name="webBrowser" Source="http://www.google.com" Margin="0" />
                    </Grid>
                </Grid>
            </local:ClosableTab>
            <local:AddableTab Background="LightBlue">
                Click '+' to add a New Tab
            </local:AddableTab>
        </TabControl>
user2039445
  • 253
  • 1
  • 4
  • 17
  • @Blachshma : Thanks for comment, but I wanna do it by backend coding. Its dynamic as you can see, it depends on selectedindex – user2039445 Feb 05 '13 at 10:13
  • Why are you creating a new Grid and then assigning a value to it? `Grid gr;` is enough. Same goes for the WebBrowser etc... – Blachshma Feb 05 '13 at 10:14
  • @user2039445 Maybe you could post the xaml markup. it will help us understand what you are trying to do – Abdusalam Ben Haj Feb 05 '13 at 10:14

1 Answers1

0

You could use a helper function to find the children by name. Try this answer

You can then call the function like this :

DependencyObject parent = tabControl1.Items[tabControl1.SelectedIndex])
WebBrowser wb = UIHelper.FindChild<WebBrowser>(parent, "WebBrowser");
wb.Source = new Uri(txtBoxUrl.Text);
Community
  • 1
  • 1
Abdusalam Ben Haj
  • 5,343
  • 5
  • 31
  • 45
  • I can do this, but I am dynamically adding and closing tabs. Each Tab has its own browser control with same hierarchy. I just shown the Hierarchy not the exact hardcoded XAML. – user2039445 Feb 05 '13 at 10:23