[WPF系列]-TreeView的常用事项

本文总结了在WPF项目中使用Treeview组件时遇到的常见问题及解决方案,包括数据绑定DataBinding、DataTemplate自定义以及事件触发顺序等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引言

 

项目经常会用Treeview来组织一些具有层级结构的数据,本节就将项目使用Treeview常见的问题作一个总结。

 

DataBinding数据绑定

 

DataTemplate自定义

<HierarchicalDataTemplate DataType="{x:Type viewModels:FieldViewModel}"
                                              ItemsSource="{Binding SubViewModels}">

                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="F:" Style="{StaticResource IconTextStyle}" 
                                Background="LightCoral"/>
                        <TextBlock Text="{Binding CurrentEntity.Value.Name}" Focusable="True">
                            <TextBlock.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="New Well" Command="{Binding AddCommand}"
                                          CommandParameter="{Binding Path=CurrentEntity.Value}" />
                                    <MenuItem Header="Delete" Command="{Binding DeleteCommand}"
                                          CommandParameter="{Binding Path=CurrentEntity.Value}" />
                                </ContextMenu>
                            </TextBlock.ContextMenu>
                        </TextBlock>
                    </StackPanel>
                </HierarchicalDataTemplate>

Style自定义

 

 

 

Events顺序

SelectedItemChanged 发生在TreeView向新selectedItem聚焦(set focus on its new selected item).

一句话: selectedItemChanged –>set foucs on the new selected item.

源自:https://social.msdn.microsoft.com/Forums/en-US/e41ec0e1-f63e-40a3-bfea-1d61d0cfcf1e/set-focus-on-textbox-after-click-on-treeviewselecteditemchanged?forum=wpf&prof=required

 

参考

[WPF系列]-数据邦定之DataTemplate 对分层数据的支持

转载于:https://www.cnblogs.com/HQFZ/p/4142461.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值