<Window x:Class="TsyCreateProjectContent.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TsyCreateProjectContent"
mc:Ignorable="d"
Loaded="Window_Loaded_V1"
Title="test" Height="525" Width="540" ResizeMode="NoResize">

<Grid Height="525" VerticalAlignment="Top" ShowGridLines="False">

<TreeView Grid.Row="0" Grid.Column="0" x:Name="FolderView" Canvas.Top="1" Canvas.Bottom="1" VerticalAlignment="Stretch"
MouseLeftButtonUp="FolderView_MouseLeftButtonUp" Margin="5,5,15,5" Width="500">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:TreeViewData}" ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal">
<Image Margin="3" Width="20" Source="{Binding ImgName}"/>
<TextBlock VerticalAlignment="Center" Text="{Binding HeaderName }" Tag="{Binding TagValue}"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
<Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
</Style>
</TreeView.ItemContainerStyle>

</TreeView>

</Grid>
</Window>

private void Window_Loaded_V1(object sender, RoutedEventArgs e)
{
List<TreeViewData> rootList = new List<TreeViewData>();
TreeViewData rootItem = new TreeViewData();
rootItem.HeaderName = "我的电脑";
rootItem.TagValue = "我的电脑";
rootItem.ImgName = "Images/windows.png";
rootItem.IsExpanded = true;
rootItem.IsSelected = true;
rootItem.Children = new List<TreeViewData>();
rootList.Add(rootItem);

// 将其添加到主树视图
//FolderView.Items.Add(rootItem);
DriveInfo[] drivers = DriveInfo.GetDrives();

// 获取电脑上的每个逻辑驱动器
foreach (var drive in Directory.GetLogicalDrives())
{
var childItem = new TreeViewData();
childItem.HeaderName = drive;
childItem.TagValue = drive;
childItem.ImgName = "Images/drive.png";
rootItem.IsExpanded = true;
rootItem.IsSelected = false;
childItem.Children = new List<TreeViewData>();
rootItem.Children.Add(childItem);
}
FolderView.ItemsSource = rootList;

}

wpf TreeView 数据绑定的更多相关文章

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

    1.先上效果 将所有节点加入ComboBox数据源,在ComboBox中选择时下方Treeview显示该节点下的子节点. 1.xaml文件,将以下代码加入界面合适位置 <StackPanel&g ...

  2. WPF:数据绑定总结(1) https://segmentfault.com/a/1190000012981745

    WPF:数据绑定总结(1) visual-studio c# 1.3k 次阅读  ·  读完需要 16 分钟 0 一.概念:什么是数据绑定? WPF中的数据绑定:是在应用程序 UI 与业务逻辑之间建立 ...

  3. C# 关于变量使用范围容易犯错的问题(TreeView数据绑定为例)

    asp.net做一个treeview数据绑定 绑定子节点时查询出来的数据正确,但在进行数据绑定时一直索引溢出 然后调试 ... 调试 ... 再调试... 依然很崩溃  想到了是变量定义后面共用后的问 ...

  4. WPF TreeView SelectedItemChanged called twice

    How to avoid WPF TreeView SelectedItemChanged being called twice Very often, we need to execute some ...

  5. winform treeView 数据绑定

    转载:http://www.jetwu.cn/archives/737 winform treeView 数据绑定 private void Form1_Load(object sender, Eve ...

  6. wpf Content数据绑定StringFormat起作用的原理和解决

    原文:wpf Content数据绑定StringFormat起作用的原理和解决 <Window x:Class="WpfOne.Bind.Bind6" xmlns=" ...

  7. WPF TreeView HierarchicalDataTemplate

    原文 WPF TreeView HierarchicalDataTemplate HierarchicalDataTemplate 的DataType是本层的绑定,而ItemsSource是绑定下层的 ...

  8. WPF TreeView Indent 减少节点的缩进

    www.swack.cn - 原文链接:WPF TreeView Indent 减少节点的缩进 问题 最近一个需求,需要在界面中实现Windows资源管理器TreeView的界面.但是我发现,我做出的 ...

  9. WPF+MVVM数据绑定问题集锦

    1.  数据绑定的问题 在使用数据绑定时,一般使用 ObservableCollection<T> 类,不使用list列表集合,因为list数据发生变化时,UI界面不更新,而Observa ...

  10. WPF TreeView的使用

    WPF提供了treeView控件,利用该控件开发者可以将数据分层显示在树结构中.当然其中需要用到Binding的机制,有用的类包括:ObjectDataProvider.DataTemplate.Hi ...

随机推荐

  1. django我学习的第一个web框架

    第一个APP----------联通后台管理系统 第二个APP----------baby商场

  2. Java语言输出菱形图型

    package fuxi;public class Diamond {    public static void main(String[] args) {        printHollowRh ...

  3. 1405. 最长快乐字符串 (Medium)

    问题描述 1405. 最长快乐字符串 (Medium) 如果字符串中不含有任何 'aaa', 'bbb' 或 'ccc' 这样的字符串作为子串,那么该字符串就是一个「快乐字符串」. 给你三个整数 a, ...

  4. Prometheus学习笔记之设置存储时间为30天不生效

    0x00 概述 Prometheus升级到2.23进行测试发现,设置存储时间为30天未生效,根据官方说明手册,实际运行后发现数据只能存储几个小时.... --storage.tsdb.retentio ...

  5. leetcode 31. 下一个排列 【时间击败 100%】 【内存击败 92.17%】

    对于该题,我本来兴致勃勃地想到了两个优化,但从提交结果来看根本看不出来有什么区别,但我发4我说的都是真的 -3- 1 public void nextPermutation(int[] nums) { ...

  6. PHP面向对象(二)

    构造函数 PHP 5 允行开发者在一个类中定义一个方法作为构造函数.具有构造函数的类会在每次创建新对象时先调用此方法,所以非常适合在使用对象之前做一些初始化工作. 代码如下: <?php//类的 ...

  7. golang 数组(array)

    1. 概念 golang中的数组是具有固定长度及相同数据类型的序列集合 2. 初始化数组 var 数组名 [数组大小]数据类型 package main import "fmt" ...

  8. reset slave

    reset slave 所有中继日志文件都被删除,即使它们还没有被复制 SQL 线程完全执行. reset slave all 所有中继日志文件都被删除,它会清除连接参数(需要重新change mas ...

  9. 2022-05-20内部群每日三题-清辉PMP

    1.一位团队成员通知项目经理有一个问题可能会破坏项目.项目经理将该问题添加到问题日志中,并要求团队找到解决方案. 项目经理下一步应该怎么做? A.更新风险登记册 B.修订项目管理计划 C.确定适当的风 ...

  10. locust自定义负载策略。

    1.时间峰值策略 每运行一分钟启动100个用户,总运行时间为10分钟 class CustomShape(LoadTestShape): # 设置时限 time_limit = 600 # 设置产生率 ...