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. 初识 go 语言:数据类型

    目录 数据类型 指针 结构体 数组 切片 切片的方法 映射 函数闭包 结束语 前言: go语言的第三篇文章,主要讲述go语言中的数据类型,包括指针,结构体,数组,切片,映射,函数闭包等,每个都提供了示 ...

  2. css 函数

    css还有一些强大的函数: 1. calc 可以混合多种单位来计算 div { font-size: calc(100vw/5 + 1rem - 100px) } 2. max.min.clamp m ...

  3. python django(forms组件)

    forms组件最大的作用,就是做数据校验. 普通做法,一个一个写校验规则,没有解耦.校验规则,都在视图函数里面. 网页校验 修改urls.py,增加路径addbook from app01 impor ...

  4. [Android] 免费天气预报接口

    [Android] 免费天气预报接口 这是 国家气象局提供的天气预报接口 [免费] 当然,网上有很多的收费API或者每天定次数的接口 使用 国家气象局 的步骤如下: 1.首先获取城市ID号 北京:10 ...

  5. 未能找到类型或命名空间名称“Quartz”

    C# 项目中使用Quartz必须使用.NetFrameWork4,而不能使用Client,否则的话会出现如题所示错误.

  6. H5取经之路——HTML的基本标签

    一.head中的基本标签 1.HTML文档的结构:     a.<head>头部部分,b.<body>主体部分 <!DOCTYPE html> <!-- ↑为 ...

  7. 软件测试之adb命令-实际公司使用场景--今日log

    软件测试之adb命令-实际公司使用场景--今日log Dotest-董浩整理 1)可以看内存泄漏: 2)可以安装.卸载app--截图并提交bug: 3)可以通过抓app日志定位问题: 4)可以结合mo ...

  8. MySql流程控制结构

    序号 结构名称 说明 1 顺序结构 程序从上往下依次执行 2 分支结构 程序从两条或多条路径中选择一条去执行 3 循环结构 程序在满足一定条件的基础上,重复执行一段代码 ⒈顺序结构 你啥都不干默认就是 ...

  9. 038_nginx backlog配置

    一. backlog=number sets the backlog parameter in the listen() call that limits the maximum length for ...

  10. Hadoop cloudera版和Apache(原生态)的区别

    ---------------------------------------------------------------------------------------------------- ...