相关疑难解决方法(0)

在WPF中数据绑定TabControl时,如何使用自定义TabItem控件?

我有一个派生自定义控件TabItem,我想将该自定义数据绑定TabItem到一个股票TabControl.我宁愿避免TabControl为这种罕见的情况创建一个新的.

这就是我所拥有的,并且我没有运气得到正确的控件来加载.在这种情况下,我想使用我的ClosableTabItem控件而不是库存TabItem控件.

<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>
Run Code Online (Sandbox Code Playgroud)

编辑:这是我最终得到的,而不是尝试绑定自定义控件.我从前一个问题得到的" CloseCommand " .

    <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" …
Run Code Online (Sandbox Code Playgroud)

wpf user-interface styles itemtemplate .net-3.5

4
推荐指数
1
解决办法
4339
查看次数

WTF WPF TabControl?

我相信这是WPF中的一个错误(v4.0,如果它很重要),但它已经很晚了,也许我错过了一些东西.

为了说明的目的,我绑定了一个假的例子:

    <x:Array x:Key="SampleItems" Type="sys:String">
        <sys:String>Foo</sys:String>
        <sys:String>Bar</sys:String>
        <sys:String>Baz</sys:String>
    </x:Array>
Run Code Online (Sandbox Code Playgroud)

这将工作并显示三个具有相同标题和内容的选项卡:

<TabControl ItemsSource="{StaticResource SampleItems}">
            <TabControl.ItemContainerStyle>
                <Style TargetType="TabItem">
                    <Setter Property="Header" Value="{Binding}" />
                    <Setter Property="Content" Value="{Binding}" />
                </Style>
            </TabControl.ItemContainerStyle>
        </TabControl>
Run Code Online (Sandbox Code Playgroud)

但是,这会抛出一个异常,消息"错误10指定的元素已经是另一个元素的逻辑子元素.首先断开它.":

<TabControl ItemsSource="{StaticResource SampleItems}">
    <TabControl.ItemContainerStyle>
        <Style TargetType="TabItem">
            <Setter Property="Header">
                <Setter.Value>
                    <!-- Anything here causes this problem. -->
                    <TextBlock Text="{Binding}"/>
                </Setter.Value>
            </Setter>
            <Setter Property="Content" Value="{Binding}" />
        </Style>
    </TabControl.ItemContainerStyle>
</TabControl>
Run Code Online (Sandbox Code Playgroud)

重要的是要注意,这可以与TextBlock中的任何文本重现.实际上,我可以用任何XAML替换标题TextBlock并获取此消息.我无法解释这一点.任何想法,或者这只是一个错误?

问题出现在VS设计器中,但这里也是运行时相关堆栈跟踪的一部分:

   at System.Windows.FrameworkElement.ChangeLogicalParent(DependencyObject newParent)
   at System.Windows.FrameworkElement.AddLogicalChild(Object child)
   at System.Windows.Controls.HeaderedContentControl.OnHeaderChanged(Object oldHeader, Object newHeader)
   at System.Windows.Controls.HeaderedContentControl.OnHeaderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at …
Run Code Online (Sandbox Code Playgroud)

wpf xaml wpf-controls xamlparseexception

3
推荐指数
1
解决办法
2406
查看次数