我有一个自定义标签控件,它在TabItem中显示一个框架,以及一个自定义标题。在ItemContainerStyle中设置绑定以显示TabItem有效,但我无法在DataTemplate中获取绑定以将标题文本设置为正常工作。请注意,TabControl绑定到ObservableCollection项,其中包含两个属性 - TI,TabItem中显示的帧,DisplayName,即标题中显示的字符串。
=============== XAML =====
<TabControl Height="Auto" Name="mainTabControl" Width="Auto" IsSynchronizedWithCurrentItem="True" Margin="4" >
<TabControl.ItemTemplate>
<DataTemplate>
<DockPanel Width="Auto">
<Button
Content="X"
Cursor="Hand"
DockPanel.Dock="Right"
Focusable="False"
FontFamily="Courier"
FontSize="9"
FontWeight="Bold"
Margin="0,1,0,0"
Padding="0"
VerticalContentAlignment="Bottom"
Width="16" Height="16"
>
<!-- Command="" //Command binding TBD later
CommandParameter="" -->
</Button>
<ContentPresenter
Content ="{Binding DisplayName}"
VerticalAlignment="Center"
Margin="0,0,7,0"
/>
</DockPanel>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.Style>
<Style TargetType="TabControl">
<Style.Triggers>
<DataTrigger Binding="{Binding Items.Count, RelativeSource={RelativeSource Self}}"
Value="0">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
</TabControl.Style>
<TabControl.ItemContainerStyle >
<Style TargetType="TabItem">
<Setter Property="Content" Value="{Binding TI}"/>
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
============ BOUND ITEM =============
class TabItemContainer : Frame
{
public event DependencyPropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new DependencyPropertyChangedEventArgs());
}
}
private string displayname;
public string DisplayName
{
get { return displayname; }
set
{
if (value != this.displayname)
{
displayname = value;
NotifyPropertyChanged("DisplayName");
}
}
}
public Frame TI { get; set; }
}
===========代码背后=============
ObservableCollection<TabItemContainer> Tablist;
public MainWindow()
{
InitializeComponent();
Tablist = new ObservableCollection<TabItemContainer>();
TabItemContainer tic = new TabItemContainer();
tic.DataContext = tic;
tic.DisplayName = "Untitled";
tic.TI = new Frame();
tic.TI.Navigate(new Page1());
Tablist.Add(tic);
mainTabControl.ItemsSource = Tablist;
mainTabControl.SelectedIndex = Tablist.Count - 1;
}
答案 0 :(得分:1)
为您的TabItem样式添加Header
绑定
<TabControl.ItemContainerStyle>
<Style TargetType="TabItem">
<Setter Property="Content" Value="{Binding TI}"/>
<Setter Property="Header" Value="{Binding }" />
</Style>
</TabControl.ItemContainerStyle>
标题中的DataContext不会自动设置为TabItemContainer
(我认为它确实......必须弄错)