0

我有以下代码 -

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);

但是我能够满足我的要求,但所有元素都是硬编码的。我知道有更好的方法和一行代码。请建议一个标准代码来执行此操作。

更新

<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>
4

1 回答 1

0

您可以使用辅助函数按名称查找孩子。试试这个答案

然后,您可以像这样调用该函数:

DependencyObject parent = tabControl1.Items[tabControl1.SelectedIndex])
WebBrowser wb = UIHelper.FindChild<WebBrowser>(parent, "WebBrowser");
wb.Source = new Uri(txtBoxUrl.Text);
于 2013-02-05T10:20:56.547 回答