How can I use a custom TabItem control when linking data from a TabControl to WPF?

综合技术 2018-12-07 阅读原文

I have a custom control that is derived from TabItem
, and I want to databind that custom TabItem
to a stock TabControl
. I would rather avoid creating a new TabControl
just for this rare case.

This is what I have and I'm not having any luck getting the correct control to be loaded. In this case I want to use my ClosableTabItem
control instead of the stock TabItem

<TabControl x:Name="tabCases" IsSynchronizedWithCurrentItem="True"
            Controls:ClosableTabItem.TabClose="TabClosed" >
        <DataTemplate DataType="{x:Type Controls:ClosableTabItem}" >
            <TextBlock Text="{Binding Path=Id}" />
        <DataTemplate DataType="{x:Type Entities:Case}">
            <CallLog:CaseReadOnlyDisplay DataContext="{Binding}" />

EDIT:This is what I ended up with, rather than trying to bind a custom control. The "CloseCommand" im getting from a previous question.

<Style TargetType="{x:Type TabItem}" BasedOn="{StaticResource {x:Type TabItem}}" >
        <Setter Property="Template">
                <ControlTemplate TargetType="{x:Type TabItem}">
                        <StackPanel Orientation="Horizontal">
                        <ContentPresenter x:Name="ContentSite"
                                Command="{Binding Path=CloseCommand}"
                                <Image Source="/Russound.Windows;component/Resources/Delete.png" Height="10" />
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="FontWeight" Value="Bold" />
                            <Setter TargetName="Border" Property="Background" Value="LightBlue" />
                            <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" />
                            <Setter TargetName="Border" Property="BorderBrush" Value="DarkBlue" />

You don't want to set the DataType
of the DataTemplate
in this case. The value of the ItemTemplate
property is used whenever a new item needs to be added, and in the case of a tab control it will be used to create a new TabItem
. You should declare an instance of your class within the DataTemplate

<TabControl x:Name="tabCases" IsSynchronizedWithCurrentItem="True" Controls:ClosableTabItem.TabClose="TabClosed">
                <TextBlock Text="{Binding Path=Id}" />
        <DataTemplate DataType="{x:Type Entities:Case}">
            <CallLog:CaseReadOnlyDisplay DataContext="{Binding}" />

This will cause a new ClosableTabItem
to be created whenever a new tab is added to the TabControl

Update; From your comment, it sounds like that the ItemTemplate
controls what is created within the TabItem
, rather than changing the TabItem
itself. To do what you want to do, but for a TreeView
, you would set the HeaderTemplate
. Unfortunately, I don't see a HeaderTemplate
property of TabControl

I did some searching, and this tutorial
modifies the contents of the tab headers by adding controls to TabItem.Header
. Maybe you could create a Style
for your TabItems
that would add the close button that your class is currently adding?

Hello, buddy!

责编内容by:Hello, buddy!阅读原文】。感谢您的支持!


WPF 使用 WindowChrome,在自定义窗口标题栏的同时最大程度保留原生窗口样式(类似 UW... WPF 自定义窗口样式有多种方式,不过基本核心实现都是在修改 Win32 窗口样式。然而,Windows 上的应用就应该有 Windows 应用的样子嘛,在保证自定义的同时也能与其他窗口样式保持一致当然能最大程度保证 Windows 操作系...
WPF: SelectedItems with duplicate object... So lets say I have these classes: public class Person { public string Name { get; set; } } public class PersonCol...
Easier Editing with New Format Painter in UI for W... Our Telerik UI for WPFandUI for Silverlightsuites have a new feature - the Format Painter. Learn the functionalities...
The static property link works only once initially I'm having problems binding a static string property to textblock in a WPF application. Initially it sets the value c...
WPF 使用 Direct2D1 画图 绘制基本图形 本文来告诉大家如何在 Direct2D1 绘制基本图形,包括线段、矩形、椭圆 本文是一个系列 WPF 使用 Direct2D1 画图 本文的组织参考 Direct2D ,对大神表示感谢。 点 在 D...