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;
     

  • 相关阅读:
    Python标准库之csv(1)
    python3:csv的读写
    Python os模块方法
    Python闭包
    Python修饰器
    Python生成器
    Python迭代器
    Python文件对象方法
    Justep X5 Studio,业界公认第一的快速开发平台
    马云:早九晚五的工作方式在2013-2015年就是慢性自杀
  • 原文地址:https://www.cnblogs.com/tianya/p/2182336.html
Copyright © 2011-2022 走看看