【问题标题】:TreeView with two collections per model每个模型有两个集合的 TreeView
【发布时间】:2016-10-15 14:10:21
【问题描述】:

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

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)

【问题讨论】:

  • 它们将如何呈现,按顺序呈现,或以类似联合的方式呈现?
  • 我用一个例子更新了帖子。
  • 抱歉,不能用你的第三个代码块来解决我的问题,它看起来是递归的,但是......这棵树到底应该做什么/帮助你?
  • 我有需要查看的数据结构。看看这个类,它是如何拥有两个自己类型的集合的。直到运行时我才知道大小。

标签: c# wpf mvvm treeview


【解决方案1】:

虽然这不能解决我原来的问题,但这是一种解决方法。

摆脱 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 2021-09-05
    • 2016-08-09
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多