定义树节点,(编译环境VS2017)
public class GBTreeNode : INotifyPropertyChanged
{
private string _deviceId = string.Empty;
private string _name = string.Empty;
private string _parentId = string.Empty;
private ImageSource _imageSource = null;
private ObservableCollection<GBTreeNode> _Nodes = new ObservableCollection<GBTreeNode>();
public string DeviceId
{
set => UpdateProperty(ref _deviceId, value);
get => _deviceId;
}
public string Name
{
set => UpdateProperty(ref _name, value);
get => _name;
}
public string ParentId
{
set => UpdateProperty(ref _parentId, value);
get => _parentId;
}
public ImageSource ImageSource
{
set => UpdateProperty(ref _imageSource, value);
get => _imageSource;
}
public ObservableCollection<GBTreeNode> Nodes
{
set => UpdateProperty(ref _Nodes, value);
get => _Nodes;
}
public void UpdateProperty<T>(ref T properValue, T newValue, [CallerMemberName] string propertyName = "")
{
if (object.Equals(properValue, newValue))
{
return;
}
properValue = newValue; OnPropertyChanged(propertyName);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName]string propertyName = "")
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
xmal 前端代码: localCatalog 绑定的树结构
<UserControl.Resources>
<HierarchicalDataTemplate x:Key="TreeViewTemplate" DataType="{x:Type mode:GBTreeNode}" ItemsSource="{Binding Nodes, Mode=TwoWay}">
<StackPanel Orientation="Horizontal" Margin="0,2,0,2">
<Image Source="{Binding ImageSource , Mode=TwoWay}" Width="16" Height="16" />
<Label Width="5" Opacity="0"></Label>
<TextBlock Text="{Binding Name, Mode=TwoWay}" ToolTip="{Binding Name, Mode=TwoWay}" FontSize="12"/>
</StackPanel>
</HierarchicalDataTemplate>
</UserControl.Resources>
<TreeView Background="#DBDDE1" ItemTemplate="{StaticResource ResourceKey=TreeViewTemplate}" BorderThickness="1"
BorderBrush="Red" ItemsSource="{Binding Path=localCatalog}" Margin="10,0,0,0" />
Tips: 编译的时候 DataType="{x:Type mode:GBTreeNode}" 此段代码会出错, 需要在xmal头添加GBTreeNode所在命名空间
xmlns:mode="clr-namespace:XXXXXXXXXXX"
CS中的后台代码: 生成目录树结构
public ObservableCollection<GBTreeNode> localCatalog = new ObservableCollection<GBTreeNode>();
localCatalog = Bind(所有的树节点);
//绑定树
private ObservableCollection<GBTreeNode> Bind(ObservableCollection<GBTreeNode> nodes)
{
ObservableCollection<GBTreeNode> outputList = new ObservableCollection<GBTreeNode>();
for (int i = 0; i < nodes.Count; i++)
{
if (FindDownward(nodes, nodes[i].ParentId) != null)
{
FindDownward(nodes, nodes[i].ParentId).Nodes.Add(nodes[i]);
}
else //找不到父节点,此节点就为根节点
{
outputList.Add(nodes[i]);
}
}
return outputList;
}
//向下查找
private GBTreeNode FindDownward(ObservableCollection<GBTreeNode> nodes, string ParentId)
{
if (nodes.Count == 0) return null;
for (int i = 0; i < nodes.Count; i++)
{
if (nodes[i].DeviceId.Equals(ParentId))
{
return nodes[i];
}
GBTreeNode node = FindDownward(nodes[i].Nodes, ParentId);
if (node != null)
{
return node;
}
}
return null;
}
CS后台代码: 遍历目录树结构
//nodes: 创建好的树结构 list: 保存树所有的树节点
private void RecurPackageTreeNodeXml(ObservableCollection<GBTreeNode> nodes, List<CatalogConfigItemType> list)
{
if (nodes.Count == 0) return;
foreach (GBTreeNode item in nodes)
{
/*
业务代码
*/
RecurPackageTreeNodeXml(item.Nodes, list);
list.Add(itemNode);
}
}