WPF 制作带TreeView的ComBox

来源:互联网 发布:mac怎么给ps安装字体 编辑:程序博客网 时间:2024/05/18 18:55

1、xaml代码部分

 <ComboBox>            <ComboBoxItem>                <ComboBoxItem.Template>                    <ControlTemplate>                        <TreeView ItemsSource="{Binding DataContext.ComBoxTreeViewData,ElementName=myWin}">                            <it:Interaction.Triggers>                                <it:EventTrigger EventName="SelectedItemChanged">                                    <it:InvokeCommandAction Command="{Binding TreeViewNodeSelectedChangedCommand}"/>                                </it:EventTrigger>                            </it:Interaction.Triggers>                            <TreeView.ItemTemplate>                                <HierarchicalDataTemplate ItemsSource="{Binding Children}">                                    <TextBlock Text="{Binding Name}"/>                                </HierarchicalDataTemplate>                            </TreeView.ItemTemplate>                        </TreeView>                    </ControlTemplate>                </ComboBoxItem.Template>            </ComboBoxItem>        </ComboBox>
2、创建一个VM以及树对应的model然后进行绑定即可

3、创建命令TreeViewNodeSelectedChangedCommand用于将选择的节点的信息显示在ComBox中。

4、为了将信息显示在comBox中可以创建一个textBlock覆盖在ComBox中。即ComBox本身不显示任何值只是作为一个容器使用。


0 0
原创粉丝点击