VB.NET WPF 2 TreeViews 相同的 ItemsSource

VB.NET WPF 2 TreeViews same ItemsSource

目前我正在尝试为 TreeView 提供相同的 TreeViewItem。

但它只在其中一个中可见。

我不知道为什么...

MainWindow.xaml:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TreeView x:Name="t1" HorizontalAlignment="Left" Height="300" Margin="10,10,0,0" VerticalAlignment="Top" Width="238"/>
        <TreeView x:Name="t2" HorizontalAlignment="Left" Height="300" Margin="10,10,0,0" VerticalAlignment="Top" Width="239" Grid.Column="1"/>

    </Grid>
</Window>

MainWindow.xaml.vb:

Class MainWindow

    Private root As TreeViewItem

    Public Sub New()

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        root = New TreeViewItem()
        Dim obj1 As TreeViewItem = New TreeViewItem()
        Dim obj2 As TreeViewItem = New TreeViewItem()

        root.Header = "TEST"
        obj1.Header = "obj1"
        obj2.Header = "obj2"

        root.ItemsSource = {obj1, obj2}

        t1.ItemsSource = {root}
        t2.ItemsSource = {root}
    End Sub
End Class

原因是 TreeViewItem 已经是 TreeView 使用的视觉对象。如果您使用任何其他自定义对象并将它们格式化为您想要在 TreeViewItemTemplate 中显示的内容,您将能够使用相同的对象。

但是你不能用 TreeViewItems 做到这一点。