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. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位dp)

    题目链接:https://ac.nowcoder.com/acm/contest/163/J 题目大意:给定一个数N,求区间[1,N]中满足可以整除它各个数位之和的数的个数.(1 ≤ N ≤ 1012 ...

  2. MySQL的SQL_Mode修改小计

    问题复现 今天突然发现MySQL服务器升级之后sql_mode变成宽松摸索了,危害如下: 临时解决 set global sql_mode='strict_trans_tables'(阿里服务器默认是 ...

  3. python学习day10 函数Ⅱ(参数&作用域)

    函数Ⅱ(参数&作用域) 知识小结: py2与py3的区别 逻辑运算()>not>and>or 字符串翻转切片翻转 is与==区别 git相关 数据类型判断 操作系统:cent ...

  4. kubernetes云平台管理实战: 自动加载到负载均衡(七)

    一.如何实现外界能访问 外界访问不了 1.启动svc [root@k8s-master ~]# cat myweb-svc.yaml apiVersion: v1 kind: Service meta ...

  5. SNMP源码分析之(一)配置文件部分

    snmpd.conf想必不陌生.在进程启动过程中会去读取配置文件中各个配置.其中几个参数需要先知道是干什么的: token:配置文件的每行的开头,例如 group MyROGroup v1 readS ...

  6. mysql MHA架构搭建过程

    [环境介绍] 系统环境:Red Hat Enterprise Linux 7 + 5.7.18 + MHA version 0.57 系统 IP 主机名 备注 版本 xx系统 192.168.142. ...

  7. [译]Ocelot - Routing

    原文 Ocelot主要的功能就是将http请求转发到对应的下游服务上去. Ocelot将一个请求路由到另外一个路由的动作叫做ReRoute.为了能让Ocelot能正常工作,需要在配置中设置ReRout ...

  8. 清除Windows访问共享时保存的凭据记录

    场景:某些时候我们连接了某台PC或服务器的共享目录或打印机,但因为一些原因突然连接不上了(或是对方组策略发生变化,或是对方计算机用户密码变更 等等..) 又或者电脑给其它用户使用,因一些安全问题需要临 ...

  9. 简单管理员权限与几个常用的PHP 常用函数,in_array(),explode(),implode(),join(),str_replace()

    先把今天要用的几个函数罗列出来: //explode()转换成数组,implode()转化成字符串 explode("分隔符",需要被分割的字符串或变量) $priv=" ...

  10. 函数语法:currentStyle、getComputedStyle兼容判断

    var oDiv = document.getElementById('aa'); if(oDiv.currentStyle){ var style = oDiv.currentStyle; aler ...