3
我有一個動態創建標籤,一個WPF應用程序,現在我想每個選項卡項目必須包含擴展和用戶控件也生病設置擴展和用戶控件DataContext的在運行時,因此,如果我在Style中定義了tabitem的模板,我覺得爲expnder和usercontrol設置datacontext會很困難。爲了HeaderTemplate中動態的TabItem與預定義模板
XAML代碼
<TabControl Name="tabDynamic" ItemsSource="{Binding}" SelectionChanged="tabDynamic_SelectionChanged" FontSize="15" FontFamily="Verdana" FontWeight="Normal" FontStretch="Expanded" >
<TabControl.Resources>
<DataTemplate x:Key="TabHeader" DataType="TabItem">
<DockPanel>
<Button Name="btnDelete" DockPanel.Dock="Right" Margin="150,0,0,0" Content="X" Foreground="WhiteSmoke" FontSize="10"
FontWeight="Bold" Padding="0" Click="btnDelete_Click" Height="15"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Name}" >
<Button.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FF9D4040" Offset="0" />
<GradientStop Color="#FFB11212" Offset="1" />
</LinearGradientBrush>
</Button.Background>
<!--<Image Source="/delete.gif" Height="11" Width="11"></Image>-->
</Button>
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=TabItem }, Path=Header}" />
</DockPanel>
</DataTemplate>
</TabControl.Resources>
</TabControl>
在代碼隱藏生病添加內容,
Private Function AddTabItem() As TabItem
Dim count As Integer = _tabItems.Count
Dim tab As New TabItem()
tab.Header = String.Format("tab{0}", count)
tab.Name = String.Format("tab{0}", count)
tab.HeaderTemplate = TryCast(tabDynamic.FindResource("TabHeader"), DataTemplate)
_tabItems.Insert(count - 1, tab)
obj = New ThumbnailImages
' add controls to tab item,
If ImageCollection.Count > 0 Then
obj.SetDataContext(ImageCollection)
tab.Content = obj
End If
Return tab
End Function
現在我需要的內容與擴展和用戶控件作爲模板,怎麼能我實現了嗎?
Thanx for your reply,this s not wat我需要..我找到了解決方案,我ahve在我的用戶控件中添加了Expander,bcz擴展器有一些複雜的邏輯,然後我在tab項中添加了usercontrol。 – Selva