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. Django模板

    Django模板系统 官方文档 常用语法 只需要记住两种特殊符号: {{  }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 变量 { 变量名 }} 变量名由字母数字和下划线组成. 点 ...

  2. 从Java角度修复SQL注入漏洞

    很多情况因为过滤不严导致很多网站存在sql注入,这里以用户登陆为例,简单举例 首先创建一个测试的数据库 比较基础,不写创建过程了 java代码如下: package cn.basic.jdbc; im ...

  3. python+turtle 笔记

    用Python+turtle绘制佩琪: from turtle import * def nose(x,y):#鼻子 penup()#提起笔 goto(x,y)#定位 pendown()#落笔,开始画 ...

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

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

  5. 优秀的电商平台Jshop栗子

    摘录自:https://blog.csdn.net/chenjun9205/article/details/52412503 下载源代码 git clone https://git.oschina.n ...

  6. JVM调优命令-jhat

    jhat JVM Heap Analysis Tool命令是与jmap搭配使用,用来分析jmap生成的dump,jhat内置了一个微型的HTTP/HTML服务器,生成dump的分析结果后,可以在浏览器 ...

  7. 使用 Google

    非原版 Glgoo:http://www.glgoo.com/谷粉搜搜:http://www.gfsoso.net/谷粉搜搜:http://www.gfsswy.com/谷粉搜搜:http://guf ...

  8. [物理学与PDEs]第2章习题10 一维理想流体力学方程组的 Lagrange 形式

    试证明: 一维理想流体力学方程组的 Lagrange 形式 (5. 22)-(5. 24) 也可写成如下形式 $$\beex \bea \cfrac{\p \tau}{\p t}-\cfrac{\p ...

  9. EffectiveC++ 第2章 构造/析构/赋值运算

    我根据自己的理解,对原文的精华部分进行了提炼,并在一些难以理解的地方加上了自己的"可能比较准确"的「翻译」. Chapter 2 构造 / 析构 / 赋值 条款 05:了解C++ ...

  10. 代码,java_web

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...