Wpf-Treeview

1.xaml

 xmlns:local="clr-namespace:YaoMinSys.Class"


 <TreeView Name="Treeview"  Padding="0" Margin="5" BorderThickness="1">
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True"></Setter>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:PropertyNodeItem}" ItemsSource="{Binding Path=Children}" >
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Text="{Binding Path=DisplayName}" Tag="{Binding Path=ID}" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown"></TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>

2.calss


namespace YaoMinSys.Class
{
class PropertyNodeItem
{
public int ID { get; set; }
public string DisplayName { get; set; }

public List<PropertyNodeItem> Children { get; set; }

public PropertyNodeItem()
{
Children = new List<PropertyNodeItem>();
}
}
}

3.xaml.cs


        private void Page_Loaded(object sender, RoutedEventArgs e)
{
ShowTreeView();
}
private void ShowTreeView()
{
List<PropertyNodeItem> itemlist = new List<PropertyNodeItem>();
try
{
YaoMinSys.BLL.ZiDianLei ZDLBll = new BLL.ZiDianLei();
DataSet ZDLDs = ZDLBll.GetList("");
for (int i = 0; i < ZDLDs.Tables[0].Rows.Count; i++)
{
PropertyNodeItem Parent = new PropertyNodeItem
{
ID = int.Parse(ZDLDs.Tables[0].Rows[i]["ZDLID"].ToString()),
DisplayName = ZDLDs.Tables[0].Rows[i]["ZDLeiMing"].ToString(),

};

YaoMinSys.BLL.ZiDianXiang ZDXBll = new BLL.ZiDianXiang();
DataSet ZDXDs = ZDXBll.GetList(" ZDLID = " + ZDLDs.Tables[0].Rows[i]["ZDLID"]);
for (int j = 0; j < ZDXDs.Tables[0].Rows.Count; j++)
{
PropertyNodeItem children = new PropertyNodeItem
{
ID = int.Parse(ZDXDs.Tables[0].Rows[j]["ZDXID"].ToString()),
DisplayName = ZDXDs.Tables[0].Rows[j]["ZDXiang"].ToString()
};
Parent.Children.Add(children);
}
itemlist.Add(Parent);
}
this.Treeview.ItemsSource = itemlist;

}
catch (Exception)
{

throw;
}
}

private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
TextBlock tb = (TextBlock)sender;
MessageBox.Show("Text :"+tb.Text+"ID :"+tb.Tag);
}

Wpf-Treeview,布布扣,bubuko.com

时间: 2025-01-05 17:03:41

Wpf-Treeview的相关文章

WPF TreeView 选择事件执行两次,获取TreeView的父节点的解决方法

1.TreeView选择事件执行两次 Very often, we need to execute some code in SelectedItemChanged depending on the selected TreeViewItem. ButSelectedItemChanged is called twice. This is due to stealing focus from the main window, which is screwing something up. Wha

WPF TreeView绑定字典集合

1 <TreeView Name="Tree" HorizontalAlignment="Left" Height="269" Width="292" > 2 3 <TreeView.ItemTemplate> 4 <HierarchicalDataTemplate ItemsSource="{Binding Value}"> 5 <StackPanel> 6

WPF TreeView HierarchicalDataTemplate

原文 WPF TreeView HierarchicalDataTemplate   <StackPanel Margin="0,0,0,0"> <StackPanel.Resources> <HierarchicalDataTemplate x:Key="MonTemp" DataType = "{x:Type local:MonthViewModel}" ItemsSource = "{Binding

WPF TreeView绑定xaml的写法(转)

WPF TreeView绑定xaml的写法 2018年05月30日 10:16:27 dxm809 阅读数:441 方法一 <Window x:Class="TreeViewDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/x

WPF TreeView 虚拟化-设置滚动到选中项

原文:WPF TreeView 虚拟化-设置滚动到选中项 前言 列表滚动到具体的数据项? ListBox提供了简易快捷的滚动定位函数ScrollIntoView. TreeView树状结构列表,则没有此类方法,无法与ListBox一样,直接设置滚动到具体的数据项. 同时,SelectedItem也是只读的,无法设置SelectedItem来间接的设置滚动项. TreeView滚动定位 1. 对TreeViewItem添加一个附加属性IsScrolledToViewWhenSelected,在属性

WPF TreeView 减少节点的缩进

WPF TreeView 减少节点的缩进 [服务器破旧,速度较慢,见谅!] 原文地址:https://www.cnblogs.com/swack/p/11363658.html

WPF treeview扩展

记录一下工作中遇到的问题,以便以后忘记了可以来看. 在工作中遇到一个问题,就是要实现类型如下的界面,没有使用Telerik和Dev库.本来最开始是想使用Datagrid,但不知道怎么实现treeview,后来遍使用treeview. 前端xaml代码: <TreeView x:Name="mytreeview"> <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Bin

WPF - TreeView 仿VS2013解决方案资源管理器中的树状结构

效果图 先上效果图,若是你想要的效果,可以继续看下面的代码,不想浪费大家的时间. 样式定义 此处定义TreeView的样式,参考自MSDN,稍作修改. 注意:在TreeViewItem控件模板定义中绑定一个数据(Level)以及一个值转换器(LevelToMarginConverter),具体定义见下部分. <Style x:Key="ExpandCollapseToggleStyle" TargetType="ToggleButton"> <Se

【C#】ADO .Net Entities Framework在WPF TreeView中的应用

XAML代码 <Window x:Class="WpfApplication73.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expre

wpf treeview 数据绑定 递归绑定节点

1.先上效果 将所有节点加入ComboBox数据源,在ComboBox中选择时下方Treeview显示该节点下的子节点. 1.xaml文件,将以下代码加入界面合适位置 1 <StackPanel> 2 <StackPanel Margin="10"> 3 <Label Content="选择组节点:"></Label> 4 <ComboBox MaxDropDownHeight="100" N