zoukankan      html  css  js  c++  java
  • wpf treeview 绑定不同的对象

    treeView 结构:

    <TreeView Name="trvMenu" IsTextSearchEnabled="True"  ItemsSource="{Binding}" Grid.Row="0">
                  <TreeView.Resources>
                    <HierarchicalDataTemplate    DataType="{x:Type models:TreeFolder}" 
    ItemsSource="{Binding Items}"
    >
                        <StackPanel Orientation="Horizontal">
                            <CheckBox IsChecked="{Binding HasRight,Mode=TwoWay}" Name="chkSourceFolder"  IsThreeState="True" ></CheckBox>
                            <TextBlock Text="{Binding Name}" Name="lbName" >:</TextBlock>
                            <TextBlock Text="{Binding Desc}" Name="lbDesc"></TextBlock>
                        </StackPanel>

               //以下是当选中是显示白色字体

                        <HierarchicalDataTemplate.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource=
                {RelativeSource Mode=FindAncestor, AncestorType=
                    {x:Type TreeViewItem}},Path=IsSelected}" Value="True">
                                <Setter TargetName="lbName" Property="Foreground" Value="White"/>
                            </DataTrigger>
                        </HierarchicalDataTemplate.Triggers>
                    </HierarchicalDataTemplate>
                    <DataTemplate DataType="{x:Type models:EnquirySourceInfo}">
                        <StackPanel Orientation="Horizontal">
                            <CheckBox IsChecked="{Binding HasRight,Mode=TwoWay}" Name="chkSourceFolder"  IsThreeState="True" ></CheckBox>
                            <TextBlock Text="{Binding Name}" Name="lbName" >:</TextBlock>
                            <TextBlock Text="{Binding Desc}" Name="lbDesc"></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type models:SystemParameterInfo}">
                        <StackPanel Orientation="Horizontal">
                            <CheckBox IsChecked="{Binding HasRight,Mode=TwoWay}" Name="chkSourceFolder"  IsThreeState="True" ></CheckBox>
                            <TextBlock Text="{Binding Name}" Name="lbName" >:</TextBlock>
                            <TextBlock Text="{Binding Desc}" Name="lbDesc"></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type models:EnquirySourceInfo}">
                        <StackPanel Orientation="Horizontal">
                            <CheckBox IsChecked="{Binding HasRight,Mode=TwoWay}" Name="chkSourceFolder"  IsThreeState="True" ></CheckBox>
                            <TextBlock Text="{Binding Name}" Name="lbName" >:</TextBlock>
                            <TextBlock Text="{Binding Desc}" Name="lbDesc"></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </TreeView.Resources>
            </TreeView>

     类结构:

     public class TreeFolder:INotifyPropertyChanged
        {
            private string strDesc;
            public string Desc { get { return strDesc; } set { strDesc = value; OnProperty("Desc"); } }
            public string Name { get; set; }
            public TreeFolder ParentFolder { get; set; }
            public IList<EnquirySourceInfo> ReportSource { get; set; }    
            public IList<Enquiry> Enquirys { get; set; }        
            public TrveeItemType ItemType { get; set; }
            public string FolderNo { get; set; }
            public string ParentNo { get; set; }

            public IList<EnquiryInfo> EnquiryInfos { get; set; }       

            public event PropertyChangedEventHandler PropertyChanged;

            public void OnProperty(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            //主要循环用来绑定时用的
            public IEnumerable<object> Items
            {
                get
                {
                    
                    if (Folders != null)
                    {
                        foreach (var group in this.Folders)
                            yield return group;
                    }

                    if (Enquirys != null)
                    {
                        foreach (var group in this.Enquirys)
                            yield return group.EnquiryIn;
                    }
                    if (ReportSource != null)
                    {
                        foreach (var entry in this.ReportSource)
                            yield return entry;
                    }
                    if (EnquiryInfos != null)
                    {
                        foreach (var info in this.EnquiryInfos)
                            yield return info;
                    }
                    
                }
            }       
        }
     绑定代码:

    trvMenu.ItemsSource = rootList;
     

  • 相关阅读:
    2017-4-25 winform公共控件属性 菜单和工具栏属性
    2017-4-24 winform窗体基础属性 ico图片生成 不规则窗体的移动 恶搞小程序
    2017-4-23 知识补充
    C# 动态方法和静态方法的区别 (转)
    2017-4-21 Ado.net字符串攻击 防御 实体类 数据访问类 属性扩展 三层架构开发
    ToString()用法 select top 子句
    2017-4-19 ado.net 增,删,改,查,练习
    2017-4-17 类库 通用变量 is和as运算符 委托
    2017-4-16 多态 构造函数 方法重载 静态方法和静态成员
    【2017-03-12】SQL Sever 子查询、聚合函数
  • 原文地址:https://www.cnblogs.com/tianya/p/2182336.html
Copyright © 2011-2022 走看看