English 中文(简体)
WPF 列表项目: 具体内容已经是另一个要素的逻辑儿童。 第1条
原标题:WPF s tab items: Specified element is already the logical child of another element. Disconnect it first

我有一支精通的TabControl公司,试图积极补充:

public class CloseableTabControl : TabControl
{
    protected override DependencyObject GetContainerForItemOverride()
    {
        return new CloseableTabItem();
    }
}  


public class CloseableTabItem : TabItem
{
    public static readonly RoutedEvent CloseTabEvent =
        EventManager.RegisterRoutedEvent("CloseTab", RoutingStrategy.Bubble,
        typeof(RoutedEventHandler), typeof(CloseableTabItem));

    public event RoutedEventHandler CloseTab
    {
        add { AddHandler(CloseTabEvent, value); }
        remove { RemoveHandler(CloseTabEvent, value); }
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        var closeButton = Template.FindName("closeButton", this) as Button;
        if (closeButton != null)
        closeButton.Click += CloseButtonClick;
    }

    void CloseButtonClick(object sender, RoutedEventArgs e)
    {
        RaiseEvent(new RoutedEventArgs(CloseTabEvent, this));
    }
}

并且XAML是:

    <local:CloseableTabControl Grid.Column="2" Grid.Row="1" SelectedIndex="{Binding SelectedTabIndex}" ItemsSource="{Binding TabItems}">
    <local:CloseableTabControl.Resources>
        <DataTemplate DataType="{x:Type TestEditor:TestEditorViewModel}">
             <TestEditor:TestEditorView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type TestParameterEditor:TestParameterEditorViewModel}">
             <TestParameterEditor:TestParameterEditorView/>
        </DataTemplate>
    </local:CloseableTabControl.Resources>
    <local:CloseableTabControl.ItemContainerStyle>
        <Style TargetType="local:CloseableTabItem">
            <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <Grid>
                        <Border Name="Border" Background="{DynamicResource Brush2}" BorderBrush="{DynamicResource Brush5}" BorderThickness="1,1,1,1" CornerRadius="6,6,0,0" >
                            <DockPanel>
                                <ContentPresenter x:Name="ContentSite"
                                     VerticalAlignment="Center"
                                     HorizontalAlignment="Center"
                                     ContentSource="Header"
                                     Margin="12,2,12,2"/>
                                <Button Name="closeButton" 
                                     HorizontalAlignment="Center" 
                                     Margin="3,0,3,0" 
                                     VerticalAlignment="Center" 
                                     Width="16" 
                                     Height="16" 
                                     DockPanel.Dock="Right" 
                                     Style="{DynamicResource CloseableTabItemButtonStyle}" 
                                     ToolTip="Close Tab">
                                     <Path Stretch="Fill" StrokeThickness="0.5" Stroke="#FF333333" Fill="#FF969696" Data="F1 M 2.28484e-007,1.33331L 1.33333,0L 4.00001,2.66669L 6.66667,6.10352e-005L 8,1.33331L 5.33334,4L 8,6.66669L 6.66667,8L 4,5.33331L 1.33333,8L 1.086e-007,6.66669L 2.66667,4L 2.28484e-007,1.33331 Z " HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
                                </Button>
                            </DockPanel>
                         </Border>
                      </Grid>
                      <ControlTemplate.Triggers>
                          <Trigger Property="IsSelected" Value="True">
                              <Setter TargetName="Border" Property="Background" Value="{DynamicResource lightBrush}" />
                          </Trigger>
                          <Trigger Property="IsSelected" Value="False">
                              <Setter TargetName="Border" Property="Background" Value="{DynamicResource Brush1}" />
                          </Trigger>
                      </ControlTemplate.Triggers>
                  </ControlTemplate>
               </Setter.Value>
            </Setter>
            <Setter Property="Header">
                <Setter.Value>
                    <StackPanel Orientation="Horizontal" ToolTip="{Binding Caption}">
                         <Image Height="18" Source="{Binding ImageName}"/>
                         <TextBlock Text="{Binding Title}" Margin="2,0,0,0"/>
                    </StackPanel>
                </Setter.Value>
            </Setter>
         </Style>
      </local:CloseableTabControl.ItemContainerStyle>
  </local:CloseableTabControl>  

当我只在这个窗子中添加一个字塔时,所有东西都是罚款的:

TabItems.Add(TestEditorViewModel);

but when I m adding more than one I m getting the mentioned exception.
I ve tried to follow the instructions here and separate the XAML to ItemTemplate and ContentTemplate but this caused a StackOverFlowException (that is caused by the ContentPresenter part of the template, I have no idea why).
Any ideas how should I approach this?
Thanks

最佳回答

The problem is with your Setter for the Header property. When the first TabItem has the Style applied, a new StackPanel will be created. When the second TabItem is created and has the Style applied the same StackPanel will be assigned to its Header property. In WPF a visual can only have 1 parent, so you receive the stated exception above. Instead you should define a HeaderTemplate which contains the StackPanel so that each time the Style is applied for a TabItem a new StackPanel is generated when WPF inflates the HeaderTemplate.

Please see this related post:

WTF WPF TabControl?

Cheers!

问题回答

问题是

<Setter Property="Header">
    <Setter.Value>
        <StackPanel Orientation="Horizontal" ToolTip="{Binding Caption}">
            <Image Height="18" Source="{Binding ImageName}"/>
            <TextBlock Text="{Binding Title}" Margin="2,0,0,0"/>
        </StackPanel>
    </Setter.Value>
</Setter>  

体内。 我不敢肯定它究竟是什么原因,我认为它只是出于某种原因两次确定的内容,但解决办法是使用。

 <local:CloseableTabControl.ItemTemplate>
     <DataTemplate>
         <StackPanel Orientation="Horizontal" ToolTip="{Binding Caption}">
              <Image Height="18" Source="{Binding ImageName}"/>
              <TextBlock Text="{Binding Title}" Margin="2,0,0,0"/>
         </StackPanel>
     </DataTemplate>
 </local:CloseableTabControl.ItemTemplate>

最好能解释问题究竟是什么。





相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签