0

我的同事威胁要让我今天加入 TheDailyWTF,因为我写的财产是用来用 ItemsControl 构建一个 3 层树视图的。

我给你留下足迹:

ObservableCollection<KeyValuePair<string, ObservableCollection<KeyValuePair<string, ObservableCollection<MyType>>>>>;

我的目标是创建一个 ItemsControl,它使用 Key 作为标题,使用 Value 作为 3 个级别的 ItemsSource:

<Style x:Key="filterTreeStyle" TargetType="ItemsControl">
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <controls:TreeViewItem IsExpanded="True">
                        <controls:TreeViewItem.Header>
                            <controlsToolkit:TreeViewItemCheckBox Content="{Binding Key}"/>
                        </controls:TreeViewItem.Header>
                        <ItemsControl ItemsSource="{Binding Value}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <controls:TreeViewItem>
                                    <controls:TreeViewItem.Header>
                                        <controlsToolkit:TreeViewItemCheckBox Content="{Binding Key}"/>
                                    </controls:TreeViewItem.Header>
                                        <controlsToolkit:TreeViewItemCheckBox IsChecked="{Binding Enabled}" Content="{Binding FilterTypeText}"/>
                                    </controls:TreeViewItem>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </controls:TreeViewItem>
                </DataTemplate>
            </Setter.Value>
        </Setter>
       </Style>

谁能把我从 TheDailyWTF 的魔掌中拯救出来?什么是更清洁的方法来做到这一点。如果我们能找到一种使关卡数量动态变化的方法,那就太好了。

4

1 回答 1

3

呃,也许我在这里很笨,但是既然你想要一个 TreeView ......为什么不使用 TreeView 呢?您还需要使用 HierarchicalDataTemplate 而不是 vanilla DataTemplate:HDT 的内容成为 Header,ItemsSource 用于创建子节点。这也将负责使级别的数量动态化。

TreeView 内置于 WPF 中,并作为 SDK 的一部分在 Silverlight 中提供。

于 2009-12-07T23:24:21.693 回答