WPF:TreeView绑定
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绑定的更多相关文章
- WPF TreeView绑定字典集合
<TreeView Name="Tree" HorizontalAlignment="Left" Height="269" Width ...
- WPF TreeView绑定xaml的写法
方法一 <Window x:Class="TreeViewDemo.MainWindow" xmlns="http://schemas.microsoft.com/ ...
- WPF TreeView HierarchicalDataTemplate
原文 WPF TreeView HierarchicalDataTemplate HierarchicalDataTemplate 的DataType是本层的绑定,而ItemsSource是绑定下层的 ...
- WPF TreeView Indent 减少节点的缩进
www.swack.cn - 原文链接:WPF TreeView Indent 减少节点的缩进 问题 最近一个需求,需要在界面中实现Windows资源管理器TreeView的界面.但是我发现,我做出的 ...
- 【WPF】最近在学习wpf 的绑定,,
最近在学习wpf 的绑定,,1.简单的说就是版前端和后端用自己的方法给分开了2.baseVm 模型 baseCmd 命令3.命令传参修改的只是界面里的属性,而不修改其它的值4.前端改变后端, 后端改变 ...
- wpf直接绑定xml生成应用程序
目的:在vs2010下用wpf完成一个配置工具,配置文件为xml格式,xml文件作为数据源,直接和wpf前台绑定,生成exe后,运行exe能够加载同路径下的xml配置文件并显示 xml文件在项目中的设 ...
- WPF DataGrid绑定一个组合列
WPF DataGrid绑定一个组合列 前台: <Page.Resources> <local:InfoConverter x:Key="converter& ...
- WPF DataGrid 绑定行双击行命令
WPF DataGrid 绑定行双击行命令 <DataGrid ...> <DataGrid.InputBindings> <MouseBinding MouseActi ...
- WPF TreeView SelectedItemChanged called twice
How to avoid WPF TreeView SelectedItemChanged being called twice Very often, we need to execute some ...
- WPF 模板绑定父级控件内容
WPF 模板绑定父级控件内容 <Style TargetType="Button" x:Key="btn"> <Setter Property ...
随机推荐
- wp系统笔记
1.了解了justified-image-grid是wp插件,继而查看wp,wp是一个免费建站系统.内置主题和插件.博客,CMS,企业站等.php+mysql 环境至少5.0以上 2.在zh-word ...
- 实战Google深度学习框架-C3-TensorFlow入门
第三章:TensorFlow入门 TensorFlow存在计算模型,数据模型和运算模型(本文用TF代表TensorFlow) 3.1 计算模型-计算图 3.1.1 计算图的概念 TensorFlow这 ...
- Redis扩展机制
Redis扩展机制扫盲 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 关于Redis的Avalibility解决方案有很多,比如Twemproxy,Codis, 一.Twempro ...
- CMDB资产管理系统开发【day26】:Django admin
想实现的是一个表里面的字段 选择性的出现在菜单栏 1.如何自定义菜单 自定义菜单前 在asset\admin.py里添加如下代码: class NewAssetApprovalZoneAdmin(ad ...
- django - 总结 - 跨域请求
script ->jsonp跨域 浏览器的同源策略:不能跨越网站请求信息: XMLHttpRequests遵循这个规定. 因此ajax等基于XML的都不能进行跨站请求 而我们知道img,ifra ...
- JavaScript IIEF 模仿块级作用域
前言 JavaScript没有块级作用域的概念.但是通过IIEF 立即执行函数我们可以实现块级作用域. function outputNumbers(count){ for (var i=0; i & ...
- iTOP-iMX6开发板-Android-can测试例程介绍
TOP-iMX6开发板的 Android 源码的 can 例程包含在 Android 源码中,在“ packages/apps/”目 录下,如下图所示,这个是应用界面的源码. 如下图所示,can ...
- 大家都知道fastclick能解决300ms延迟,现在我们来看一下,使用方法
1.在终端输入以下命令进行安装 npm install fastclick -S 2.在你用脚手架搭建好的项目中,找到mian.js这个入口文件,打开 3.在其中加入: import FastClic ...
- Oracle 关键字、高级函数的使用
1.序列.唯一标识 查询时,可以添加递增序列 rownum 表的数据每一行都有一个唯一的标识 rowid 2.函数 单行:查询多条数据 如:to_date() 多行:查询总结数据,一般用于group ...
- 🍓vue & react 一些重要但没必要死记硬背的东西