我如何可以使用自定义的TabItem控制数据绑定在WPF中的TabControl的时候? [英] How can I use a custom TabItem control when databinding a TabControl in WPF?

查看:1286
本文介绍了我如何可以使用自定义的TabItem控制数据绑定在WPF中的TabControl的时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从的TabItem 派生的自定义控制,我想数据绑定的自定义的TabItem 到股票的TabControl 。我宁愿避免建立新的的TabControl 只为这难得一遇。

这是我和我没有任何运气得到正确的控制加载。在这种情况下,我想使用的股票我的 ClosableTabItem 控件,而不是的TabItem 控制。

 < TabControl的X:名称=tabCasesIsSynchronizedWithCurrentItem =真
            控制:ClosableTabItem.TabClos​​e =TabClos​​ed>
    < TabControl.ItemTemplate>
        < D​​ataTemplate中数据类型={X:类型控件:ClosableTabItem}>
            < TextBlock的文本={绑定路径= ID}/>
        < / DataTemplate中>
    < /TabControl.ItemTemplate>
    < TabControl.ContentTemplate>
        < D​​ataTemplate中数据类型={X:类型实体:案例}>
            < CallLog:CaseReadOnlyDisplay的DataContext ={结合}/>
        < / DataTemplate中>
    < /TabControl.ContentTemplate>
< / TabControl的>
 

编辑:这是我结束了,而不是试图绑定一个自定义的控制。 在<一href="http://stackoverflow.com/questions/935043/how-can-i-get-a-close-button-on-a-templated-tabitem-in-wpf">CloseCommand" IM从<一个得到href="http://stackoverflow.com/questions/935043/how-can-i-get-a-close-button-on-a-templated-tabitem-in-wpf">$p$pvious问题。

 &LT;风格的TargetType ={X:类型的TabItem}支持算法FMP ={{的StaticResource X:类型的TabItem}}&GT;
        &LT; setter属性=模板&GT;
            &LT; Setter.Value&GT;
                &LT;的ControlTemplate的TargetType ={X:类型的TabItem}&GT;
                    &LT; BORDER
                            名称=边境
                            背景=浅灰色
                            BorderBrush =黑
                            了borderThickness =1
                            CornerRadius =25,0,0,0
                            SnapsToDevicePixels =真&GT;
                        &LT; StackPanel的方向=横向&GT;
                        &LT;内容presenter X:名称=ContentSite
                              VerticalAlignment =中心
                              的Horizo​​ntalAlignment =中心
                              ContentSource =头
                              保证金=20,1,5,1/&GT;
                            &LT;按钮
                                命令={绑定路径= CloseCommand}
                                光标=手
                                DockPanel.Dock =右
                                可调焦=FALSE
                                保证金=1,1,5,1
                                背景=透明
                                了borderThickness =0&GT;
                                &lt;图像源=/ Russound.Windows;组件/资源/ Delete.png高度=10/&GT;
                            &LT; /按钮&GT;
                        &LT; / StackPanel的&GT;
                    &LT; /边框&GT;
                    &LT; ControlTemplate.Triggers&GT;
                        &LT;触发属性=IsSelectedVALUE =真&GT;
                            &LT; setter属性=粗细VALUE =大胆/&GT;
                            &LT;二传手的TargetName =边框属性=背景VALUE =浅蓝色/&GT;
                            &LT;二传手的TargetName =边框属性=了borderThicknessVALUE =1,1,1,0/&GT;
                            &LT;二传手的TargetName =边框属性=BorderBrushVALUE =深蓝色/&GT;
                        &LT; /触发&GT;
                    &LT; /ControlTemplate.Triggers>
                &LT; /控件模板&GT;
            &LT; /Setter.Value>
        &LT; /二传手&GT;
    &LT; /样式和GT;
 

解决方案

您不想设置的数据类型的DataTemplate 在这种情况下。在 ItemTemplate中属性的值时则使用一个新的项目需要添加,并在标签控制的情况下将使用它来创建一个新的的TabItem 。你应该在声明类的一个实例的DataTemplate 本身:

 &LT; TabControl的X:名称=tabCasesIsSynchronizedWithCurrentItem =真的控制:ClosableTabItem.TabClos​​e =TabClos​​ed&GT;
    &LT; TabControl.ItemTemplate&GT;
        &LT;的DataTemplate&GT;
            &LT;控制:ClosableTabItem&GT;
                &LT; TextBlock的文本={绑定路径= ID}/&GT;
            &LT; /控制:ClosableTabItem&GT;
        &LT; / DataTemplate中&GT;
    &LT; /TabControl.ItemTemplate>
    &LT; TabControl.ContentTemplate&GT;
        &LT; D​​ataTemplate中数据类型={X:类型实体:案例}&GT;
            &LT; CallLog:CaseReadOnlyDisplay的DataContext ={结合}/&GT;
        &LT; / DataTemplate中&GT;
    &LT; /TabControl.ContentTemplate>
&LT; / TabControl的&GT;
 

这将导致一个新的 ClosableTabItem 每当一个新的选项卡添加到要创建的的TabControl

更新;从你的评论,这听起来像的 ItemTemplate中控制什么中创建的的TabItem ,而不是改变的TabItem 本身。做你想做的事,但对于的TreeView ,您将设置 HeaderTemplate中。不幸的是,我没有看到的 HeaderTemplate中属性的TabControl

我做了一些搜索和本教程通过将控件添加到 TabItem.Header 修改标签头的内容。也许你可以创建一个风格的TabItems ,将添加关闭按钮,你的班级正在增加?

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 control.

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

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">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <Border 
                            Name="Border"
                            Background="LightGray"
                            BorderBrush="Black" 
                            BorderThickness="1" 
                            CornerRadius="25,0,0,0"
                            SnapsToDevicePixels="True">
                        <StackPanel Orientation="Horizontal">
                        <ContentPresenter x:Name="ContentSite"
                              VerticalAlignment="Center"
                              HorizontalAlignment="Center"
                              ContentSource="Header"
                              Margin="20,1,5,1"/>
                            <Button 
                                Command="{Binding Path=CloseCommand}"
                                Cursor="Hand"
                                DockPanel.Dock="Right"
                                Focusable="False"
                                Margin="1,1,5,1"
                                Background="Transparent"
                                BorderThickness="0">
                                <Image Source="/Russound.Windows;component/Resources/Delete.png" Height="10" />
                            </Button>
                        </StackPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <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" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

解决方案

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 itself:

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

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?

这篇关于我如何可以使用自定义的TabItem控制数据绑定在WPF中的TabControl的时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆