namespace PostViewer
{
using System.Collections.ObjectModel;
using System.ComponentModel; /// <summary>
/// 数据类型ViewModel.
/// </summary>
public class VmTviDataType : ITreeItem
{
private bool mIsExpanded = true;
private bool mIsSelected; /// <summary>
/// Initializes a new instance of the <see cref="VmTviDataType"/> class.
/// </summary>
/// <param name="type">类型.</param>
public VmTviDataType()
{
this.TviDataList = new ObservableCollection<VmTviData>();
} /// <inheritdoc/>
public event PropertyChangedEventHandler PropertyChanged; /// <summary>
/// Gets or sets 元素名称.
/// </summary>
public string Header { get; protected set; } /// <summary>
/// Gets or sets a value indicating whether Treeviewitem 展开.
/// </summary>
public bool IsExpanded
{
get
{
return this.mIsExpanded;
} set
{
if (value != this.mIsExpanded)
{
this.mIsExpanded = value;
this.OnPropertyChanged("IsExpanded");
}
}
} /// <summary>
/// Gets or sets a value indicating whether Treeviewitem 选中.
/// </summary>
public bool IsSelected
{
get
{
return this.mIsSelected;
} set
{
if (value != this.mIsSelected)
{
this.mIsSelected = value;
this.OnPropertyChanged("IsSelected");
}
}
} /// <summary>
/// Gets or sets 数据集合.
/// </summary>
public ObservableCollection<VmTviData> TviDataList { get; set; } /// <summary>
/// 属性变更事件处理.
/// </summary>
/// <param name="v">属性名称.</param>
protected void OnPropertyChanged(string v)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(v));
}
}
}
// <copyright file="ITreeItem.cs" company="Xtech">
// Copyright (c) Xtech. All rights reserved.
// </copyright> namespace PostViewer
{
using System.ComponentModel; /// <summary>
/// 树形控件接口.
/// </summary>
public interface ITreeItem : INotifyPropertyChanged
{
/// <summary>
/// Gets or sets a value indicating whether 是否选中.
/// </summary>
bool IsSelected { get; set; } /// <summary>
/// Gets or sets a value indicating whether 是否展开.
/// </summary>
bool IsExpanded { get; set; } /// <summary>
/// Gets 元素显示名称.
/// </summary>
string Header { get; }
}
}
<UserControl x:Class="PostViewer.UcProjectTree"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PostViewer"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="300">
<Grid>
<TreeView ItemsSource="{Binding TviProjectTypes}">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="FontWeight" Value="Normal" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True" >
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:VmTviProjectType}" ItemsSource="{Binding TviDataTypes}">
<StackPanel Orientation="Horizontal" Margin="0 2 5 0">
<TextBlock Text="{Binding Header}"/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:VmTviDataType}" ItemsSource="{Binding TviDataList}">
<StackPanel Orientation="Horizontal" Margin="0 2 5 0">
<TextBlock Text="{Binding Header}" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:VmTviData}">
<StackPanel Orientation="Horizontal" Margin="0 2 5 0">
<Image Width="15" Height="15" Margin="0, 1, 0 ,0" Source="/PostViewer;component/Resources/tree_blue.png" />
<TextBlock Text="{Binding Header}" /> </StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
</UserControl>

WPF:TreeView绑定的更多相关文章

  1. WPF TreeView绑定字典集合

    <TreeView Name="Tree" HorizontalAlignment="Left" Height="269" Width ...

  2. WPF TreeView绑定xaml的写法

    方法一 <Window x:Class="TreeViewDemo.MainWindow" xmlns="http://schemas.microsoft.com/ ...

  3. WPF TreeView HierarchicalDataTemplate

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

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

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

  5. 【WPF】最近在学习wpf 的绑定,,

    最近在学习wpf 的绑定,,1.简单的说就是版前端和后端用自己的方法给分开了2.baseVm 模型 baseCmd 命令3.命令传参修改的只是界面里的属性,而不修改其它的值4.前端改变后端, 后端改变 ...

  6. wpf直接绑定xml生成应用程序

    目的:在vs2010下用wpf完成一个配置工具,配置文件为xml格式,xml文件作为数据源,直接和wpf前台绑定,生成exe后,运行exe能够加载同路径下的xml配置文件并显示 xml文件在项目中的设 ...

  7. WPF DataGrid绑定一个组合列

    WPF DataGrid绑定一个组合列 前台: <Page.Resources>        <local:InfoConverter x:Key="converter& ...

  8. WPF DataGrid 绑定行双击行命令

    WPF DataGrid 绑定行双击行命令 <DataGrid ...> <DataGrid.InputBindings> <MouseBinding MouseActi ...

  9. WPF TreeView SelectedItemChanged called twice

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

  10. WPF 模板绑定父级控件内容

    WPF 模板绑定父级控件内容 <Style TargetType="Button" x:Key="btn"> <Setter Property ...

随机推荐

  1. 用beam实现连接kafka和elasticSearch示例 在flink平台运行

    示例实现beam用java编程,监听kafka的testmsg主题,然后将收取到的单词,按5秒做一次统计.结果输出到outputmessage 的kafka主题,同时同步到elasticSearch. ...

  2. [BJOI2019] 光线

    看起来很麻烦,做起来并不难的题 以下设:$a_i=\frac{a_i}{100},b_i=\frac{b_i}{100}$ 显然,如果$b_i=0$的话,直接求$\Pi a_i$就是答案. 解决反射问 ...

  3. SQL学习指南之查询入门

    查询语句 select语句由几个组件或者说子句构成.不过在MySQL中,只有一种子句是必不可少的(select子句),通常的查询语句会至少包含6个子句中的2~3个.下面的表列出了用于不同目的的各个子句 ...

  4. Linux设备树(三 属性)

    三 属性(property) device_type = "memory"就是一个属性,等号前边是属性,后边是值.节点是一个逻辑上相对独立的实体,属性是用来描述节点特性的,根据需要 ...

  5. 休眠(1):sleep和wait的区别

    1.这两个方法来自不同的类分别是,sleep来自Thread类,和wait来自Object类. 2.sleep是Thread的静态类方法,谁调用的谁去睡觉,即使在a线程里调用了b的sleep方法,实际 ...

  6. 爬虫案例之Pubmed数据库下载

    代码 # encoding=utf-8 import os, time, re import urllib.request import urllib.parse import ssl ssl._cr ...

  7. CVE-2017-7494 Linux Samba named pipe file Open Vul Lead to DLL Execution

    catalogue . 漏洞复现 . 漏洞代码原理分析 . 漏洞利用前提 . 临时缓解 && 修复手段 1. 漏洞复现 . SMB登录上去 . 枚举共享目录,得到共享目录/文件列表,匿 ...

  8. Python list和 np.Array 的转换关系

    一.List转String 1.str list转 string a_list = ["h","e","l","l",& ...

  9. DirectX11 With Windows SDK--16 流输出阶段

    前言 在上一章,我们知道了如何使用几何着色器来重新组装图元,比如从一个三角形分裂成三个三角形.但是为了实现更高阶的分形,我们必须要从几何着色器拿到输出的顶点.这里我们可以使用可选的流输出阶段来拿到顶点 ...

  10. MD5 两次加密

    1.添加依赖 <dependency> <groupId>commons-codec</groupId> <artifactId>commons-cod ...