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;
     

  • 相关阅读:
    从远程仓库更新本地仓库
    git添加所有新文件
    删除远程仓库文件夹
    更新被驳回
    git安装和第一次提交过程
    JSONobject按照put顺序存储和读取
    Map 转 json格式 保留null值的解决办法
    Java对象转换Json的细节处理
    如何解析json格式的字符串
    json 拼装空list、object
  • 原文地址:https://www.cnblogs.com/tianya/p/2182336.html
Copyright © 2011-2022 走看看