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;
     

  • 相关阅读:
    PPR的断管
    排水地漏的功能与种类
    PPR管及管件的类型、规格与选用
    水龙头的安装、拆卸与阀芯更换
    为不同的用户生成不同的 Kibana 界面
    如何让匿名的用户访问受限的资源
    Beats processors
    Elasticsearch 开发入门
    Elasticsearch Dockerfile 例子
    燃气热水器的结构与安装
  • 原文地址:https://www.cnblogs.com/tianya/p/2182336.html
Copyright © 2011-2022 走看看