<shared:PixelSnapper VerticalAlignment="Center" HorizontalRoundMode="Floor" VerticalRoundMode="Floor"> <StackPanel Orientation="Horizontal"> <bu:StateIcon Visibility="{Binding Converter={StaticResource xCITypeOfStateIconVisiblityConverter}}" Width="12" Height="12" AOrC="all"  MonitorState="{Binding AllState}"></bu:StateIcon> <Image ToolTip="{Binding IconUrl}" Width="16" Height="16" Source="{Binding IconUrl}"  Margin="2"/> </StackPanel> </shared:PixelSnapper> <StackPanel Margin="0" Orientation="Horizontal" ToolTip="{Binding PropertyOfTreeView.ToolTip, Converter={StaticResource _ToolTipEmptyConverter}}"> <TextBlock VerticalAlignment="Center" Margin="2" Text="{Binding Title}"></TextBlock> <TextBlock VerticalAlignment="Center" Margin="2" Foreground="#0D43F6" Text="{Binding SubTitle}"></TextBlock> </StackPanel> </StackPanel> </HierarchicalDataTemplate> </UserControl.Resources> private void xTreeViewRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) TreeViewItem item = UIElementHelper.GetTemplatedAncestor<TreeViewItem>(e.OriginalSource as FrameworkElement); if (item != null) IsMouseRightButton = true; //此时会触发TreeView的SelectedItemChanged事件,IsMouseRightButton用于在事件中判断是否是鼠标右键触发选中节点改变 item.IsSelected = true; IsMouseRightButton = false; node.PropertyOfTreeView.NodeSubCount = Convert.ToInt32(con.ExecuteScalar("SELECT COUNT(0) FROM C_ORGAN")); node.PropertyOfTreeView.IsRoot = true; ParentNode.Nodes.Add(node); string sql = "SELECT (select count(0) from C_ORGAN A where A.AF_PID=B.P_GUID) as SUBCOUNT, A_ISTYPE,P_GUID,D_TEXT FROM C_ORGAN B where AF_PID='{0}' ORDER BY A_IsType, A_SORT"; if (IsRoot) sql = String.Format(sql, "root"); sql = String.Format(sql, ParentNode.Key); IDataReader reader = con.ExecuteReader(sql); while (reader.Read()) SmartCINode node = new SmartCINode(); node.Title = reader["D_TEXT"].ToString(); node.PropertyOfTreeView.NodeSubCount = Convert.ToInt32(reader["SUBCOUNT"]); node.Key = reader["P_GUID"].ToString(); node.CIType = (Convert.ToBoolean(reader["A_ISTYPE"]) ? CITypeOfCmdb.OrganizationGroup : CITypeOfCmdb.Organization); if(node.CIType == CITypeOfCmdb.Organization) node.PropertyOfTreeView.IsDetail = true; ParentNode.Nodes.Add(node); reader.Close(); WPF中TreeView控件双击不展开节点等方法原文地址:WPF中TreeView控件双击不展开节点等方法作者:mousekittyXAML文件&lt;UserControl.Resources&gt;&lt;local:CITypeOfStateIconVisiblityConverter x:Key="xCITypeOfStateIconVisiblityConverter" /&gt;&lt;local:ToolTipEmptyConve...
TreeView 控件 可在树结构 显示分层数据,其 的项可以 展开 和折叠。它可以包含多种类型的 控件 ,如Button、Lable、Image等 控件 ,可以通过绑定到数据源并使用HieratchicalDataTemplate对象来填充其树。可以修改默认ControlTemplate以使 控件 具有独特的外观。 这里举一个完整的 TreeView 实例来进行说明。本节主要实现自定义 控件 样式+数据源绑定+动态添加父...
最近项目上需要通过MVVM来控制 TreeView ,其 需要需要控制通过搜索来定位某个 节点 ,正常逻辑下,首先通过需要在树上面找到该 节点 ,然后选 节点 ,并将该 节点 的父 节点 展开 ,这个时候需要通过MVVM来控制,需要绑定起来,只是一直没有binding上,代码如下: MVVM示例代码: 1 using System; 2 using System.Collections.Ge...
void __fastcall TForm1::tvAreaManageCollapsing(TObject *Sender, TTreeNode *Node, bool &AllowCollapse) AllowCollapse = m_bAllow; //-----------------------------------------------
WPF 对树 控件 的使用主要有两种 方法 方法 一,对 TreeView 进行静态搭建,对应的XAML文件代码如下: <Window x:Class=" TreeView .MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.
expanded事件会依次作用于该结点及其祖先(因为打开到第二级后焦点立即回到父 节点 ,所以一直没有打开过第三级结点) 所以,在应用expanded事件时,要给想处理的结点添加一个空白结点,然后在expanded事件 检查这个 节点 的存在,否则就跳过 问题是出现在试着编写一个资源管理器时发现的,源码可以参考 源码...
public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 然后,我们在MainViewModel 定义一个ObservableCollection<TreeNodeViewModel>来表示树的根 节点 ,以及一些 方法 来添加/删除 节点 和处理 双击 事件: ```csharp public class MainViewModel : INotifyPropertyChanged private ObservableCollection<TreeNodeViewModel> _rootNodes; public ObservableCollection<TreeNodeViewModel> RootNodes get { return _rootNodes; } if (_rootNodes != value) _rootNodes = value; OnPropertyChanged(nameof(RootNodes)); public MainViewModel() RootNodes = new ObservableCollection<TreeNodeViewModel>(); public void AddNode(TreeNodeViewModel parent, string text) if (parent == null) RootNodes.Add(new TreeNodeViewModel { Text = text, Children = new ObservableCollection<TreeNodeViewModel>() }); parent.Children.Add(new TreeNodeViewModel { Text = text, Children = new ObservableCollection<TreeNodeViewModel>() }); public void RemoveNode(TreeNodeViewModel node) if (node == null) return; if (node == RootNodes.FirstOrDefault()) RootNodes.Remove(node); var parent = FindParentNode(RootNodes, node); if (parent != null) parent.Children.Remove(node); public void HandleDoubleClick(TreeNodeViewModel node) // handle double click event private TreeNodeViewModel FindParentNode(IEnumerable<TreeNodeViewModel> nodes, TreeNodeViewModel child) foreach (var node in nodes) if (node == child || node.Children == null) continue; if (node.Children.Contains(child)) return node; var parent = FindParentNode(node.Children, child); if (parent != null) return parent; return null; public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 最后,我们可以在View 使用 TreeView 控件 来显示树,并将其绑定到MainViewModel 的RootNodes属性。我们还可以为 TreeView Item添加 双击 事件处理程序,以及为ContextMenu添加添加/删除 节点 的菜单项。 ```xaml < TreeView ItemsSource="{Binding RootNodes}"> < TreeView .ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Children}"> <TextBlock Text="{Binding Text}" /> </HierarchicalDataTemplate> </ TreeView .ItemTemplate> < TreeView .ItemContainerStyle> <Style TargetType="{x:Type TreeView Item}"> <EventSetter Event=" Mouse DoubleClick" Handler=" TreeView Item_DoubleClick" /> <Setter Property="ContextMenu"> <Setter.Value> <ContextMenu> <MenuItem Header="Add Node" Command="{Binding AddNodeCommand}" /> <MenuItem Header="Remove Node" Command="{Binding RemoveNodeCommand}" /> </ContextMenu> </Setter.Value> </Setter> </Style> </ TreeView .ItemContainerStyle> </ TreeView >