0
私は動的に作成したい1つのListBoxと1つのWebBrowserタイプの内容でTabItemを作成します。 ヘッダー部分を正しく追加できますが、DataTemplateに複数の内容を追加する方法はわかりません。DataTemplateで複数のコンテンツをWPFで動的にバインドする方法はありますか
private TabItem AddTabItem()
{
int count = _tabItems.Count;
TabItem tab = new TabItem();
tab.Header = string.Format("Tab {0}", count);
tab.Name = string.Format("tab{0}", count);
WebBrowser wbr = new WebBrowser();
wbr.MaxHeight = 1550;
wbr.MaxWidth = 1550;
wbr.Navigate("https://google.com");
tab.Content = wbr;
//Header Part is working Fine
tab.HeaderTemplate = tabDynamic.FindResource("TabHeader") as DataTemplate;
//I Want to add below commented line in my code for Content
// tab.ContentTemplate = tabDynamic.FindResource("TabCont") as DataTemplate;
return tab;
}
<TabControl x:Name="tabDynamic" ItemsSource="{Binding}" >
<!--SelectionChanged="tabDynamic_SelectionChanged"-->
<TabControl.Resources>
<DataTemplate x:Key="TabHeader" DataType="TabItem">
<DockPanel>
<Button x:Name="btnDelete" DockPanel.Dock="Right" Margin="5,0,0,0" Padding="0" Click="btnDelete_Click" CommandParameter="{Binding Name, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}">
<Image Source="/delete.gif" Height="11" Width="11"/>
</Button>
<TextBlock Text="{Binding Header, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}" />
</DockPanel>
</DataTemplate>
<DataTemplate x:Key="TabCont" DataType="TabItem">
<!-- ========================== -->
<!-- Bind Dynamically -->
<!-- one list box-->
<!-- Webbrowser-->
<!-- ========================== -->
</DataTemplate>
</TabControl.Resources>
</TabControl>
データテンプレートに複数のコンテンツを追加することはできません。代わりに、そのテンプレートをサポートしているアイテム(StacKPanel、Grid、DockPanelなど)にコンテンツを設定することができます。意味をなさない – MikeT