gpt4 book ai didi

WPF 的选项卡项 : Specified element is already the logical child of another element. 先断开连接

转载 作者:行者123 更新时间:2023-12-02 08:52:12 25 4
gpt4 key购买 nike

我有一个带有 CloseableTabItems 的 CloseableTabControl 我正在尝试动态添加:

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>

当我在此窗口的 View 模型的构造函数中仅添加一个选项卡时,一切都很好:

TabItems.Add(TestEditorViewModel);

但是当我添加多个时,我得到了提到的异常。
我已尝试按照说明进行操作 here并将 XAML 分离到 ItemTemplateContentTemplate 但这导致了 StackOverFlowException(由 ContentPresenter 部分引起模板,我不知道为什么)。
我应该如何处理这个问题?
谢谢

最佳答案

问题出在 Header 属性的 Setter 上。当第一个 TabItem 应用了 Style 时,将创建一个新的 StackPanel。当创建第二个 TabItem 并应用 Style 时,same StackPanel 将分配给它的 header 属性。在 WPF 中,视觉对象只能有 1 个父对象,因此您会收到上述异常。相反,您应该定义一个包含 StackPanelHeaderTemplate,以便每次将 Style 应用于 TabItemnew StackPanel 是在 WPF 扩展 HeaderTemplate 时生成的。

请参阅此相关帖子:

WTF WPF TabControl?

干杯!

关于WPF 的选项卡项 : Specified element is already the logical child of another element. 先断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7703270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com