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>

原文地址:https://www.cnblogs.com/xpvincent/p/9525365.html

时间: 2024-10-11 07:11:20

WPF:TreeView绑定的相关文章

WPF TreeView绑定字典集合

1 <TreeView Name="Tree" HorizontalAlignment="Left" Height="269" Width="292" > 2 3 <TreeView.ItemTemplate> 4 <HierarchicalDataTemplate ItemsSource="{Binding Value}"> 5 <StackPanel> 6

WPF TreeView绑定xaml的写法(转)

WPF TreeView绑定xaml的写法 2018年05月30日 10:16:27 dxm809 阅读数:441 方法一 <Window x:Class="TreeViewDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/x

WPF之TreeView绑定

新建解决方案: StudentBll.cs代码: 1 public class StudentBll 2 { 3 public List<TreeItem> infoList { get; set; } 4 public StudentBll() 5 { 6 infoList = new List<TreeItem>(); 7 infoList.Add(new TreeItem() { Self=new StudentInfo(0, "人员信息", "

WPF TreeView HierarchicalDataTemplate

原文 WPF TreeView HierarchicalDataTemplate   <StackPanel Margin="0,0,0,0"> <StackPanel.Resources> <HierarchicalDataTemplate x:Key="MonTemp" DataType = "{x:Type local:MonthViewModel}" ItemsSource = "{Binding

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

目的:在vs2010下用wpf完成一个配置工具,配置文件为xml格式,xml文件作为数据源,直接和wpf前台绑定,生成exe后,运行exe能够加载同路径下的xml配置文件并显示 xml文件在项目中的设置不同: 生成操作为内容,始终复制,可以直接修改exe同目录下的xml文件,xml文件内容的变化能体现到exe启动后的界面,exe大小为16K,xml大小为3K, 生成操作为Resource,始终复制,修改xml文件,exe启动后显示内容没有变化,exe大小为18K,可见Resource时,生成的e

WPF多路绑定

多路绑定实现对数据的计算,XAML: 引用资源所在位置 xmlns:cmlib="clr-namespace:CommonLib;assembly=CommonLib"> <UserControl.Resources> <cmlib:CustomMultiValueConvertor x:Key="MultiValueConverter"/> </UserControl.Resources> <TextBlock Fo

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

最近在学习wpf 的绑定,,1.简单的说就是版前端和后端用自己的方法给分开了2.baseVm 模型 baseCmd 命令3.命令传参修改的只是界面里的属性,而不修改其它的值4.前端改变后端, 后端改变前端要用的的函数 PropertyChanged 延伸出来的 OnProperyChanged 再延伸出来的 SetProperty5.前端和后端的分离,Interaction triggers in WPFhttp://www.cnblogs.com/lynn-/p/3262658.html <i

WPF TreeView 选择事件执行两次,获取TreeView的父节点的解决方法

1.TreeView选择事件执行两次 Very often, we need to execute some code in SelectedItemChanged depending on the selected TreeViewItem. ButSelectedItemChanged is called twice. This is due to stealing focus from the main window, which is screwing something up. Wha

WPF DataGrid绑定一个组合列

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

TreeView绑定XmlDataSource

<?xml version="1.0" encoding="utf-8" ?> <movies> <category id="category1" text="Action"> <movie id="movie1" text="Star Wars" /> <movie id="movie2" text="