我有以下代码 -
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我能够满足我的要求,但所有元素都是硬编码的。 我知道有更好的方法和一行代码。请建议使用标准代码。
更新
<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>
答案 0 :(得分:0)
您可以使用辅助函数按名称查找子项。试试这个answer
然后您可以像这样调用函数:
DependencyObject parent = tabControl1.Items[tabControl1.SelectedIndex])
WebBrowser wb = UIHelper.FindChild<WebBrowser>(parent, "WebBrowser");
wb.Source = new Uri(txtBoxUrl.Text);