每个模型有两个集合的树视图

本文关键字:集合 视图 两个 模型 | 更新日期: 2023-09-27 18:13:08

我有一个类,它有两个集合,我需要在我的树中显示。

class Test
{
     ObservableCollection<Test> ReplacementOptions {get; set;}
     ObservableCollection<Test> Given {get; set;}
     public String Name {get; set;}
}

现在我只知道如何创建它与ReplacementOptions:

    <TreeView Grid.Row="1" x:Name="DefaultEquipmentTreeView" ItemsSource="{Binding SelectedUnit.DefaultEquipment}" PreviewMouseRightButtonUp="OnPreviewMouseRightButtonDown">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding ReplacementOptions}" >
                <TextBlock Text="{Binding Name}" />
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
        <i:Interaction.Behaviors>
            <xmlEditor:BindableSelectedItemBehavior SelectedItem="{Binding SelectedDefaultEquipment, Mode=TwoWay}" />
        </i:Interaction.Behaviors>
    </TreeView>

我如何能够完成在树中递归地显示两个集合与MVVM?

的例子:

如您所见,每个测试都有2个测试集合。

Test1 //Name
--Replacement //Replacements. for the collection name
   -- Test2 //Then that lists each Test Name in that collection
        --Replacements //then that Collection has both Collections... etc
             --Test4
        --Given
--Given // Given
 --Test3
    --Replacements(empty collection)
    --Given(empty collection)

每个模型有两个集合的树视图

虽然这不能解决我最初的问题,但这是一个变通方法。

去掉TreeView,使用ListView:

<ListView  x:Name="DefaultEquipmentTreeView" ItemsSource="{Binding SelectedUnit.DefaultEquipment}" SelectedItem="{Binding SelectedDefaultEquipment, Mode=TwoWay}" ItemTemplate="{StaticResource EquipmentTemplate}"/>

模板:

<DataTemplate x:Key="EquipmentTemplate" DataType="models:Equipment">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock Text="{Binding Name}" Margin="5,0,0,0"/>
        <TextBlock Grid.Row="1" Text="Replacement Options:" Margin="10,0,0,0"/>
        <ListView Grid.Row="2" ItemsSource="{Binding ReplacementOptions}" Margin="15,0,0,0" SelectedItem="{Binding SelectedDefaultEquipment, Mode=TwoWay, Source={StaticResource MainViewModel}}"/>
        <TextBlock Grid.Row="3" Text="Given:" Margin="10,0,0,0"/>
        <ListView Grid.Row="4" ItemsSource="{Binding GivenEquipment}" Margin="15,0,0,0" SelectedItem="{Binding SelectedDefaultEquipment, Mode=TwoWay, Source={StaticResource MainViewModel}}"/>
    </Grid>
</DataTemplate>