源码地址: https://gitee.com/zhang_jianli

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;
 

 

posted on 2011-09-20 12:43  天涯  阅读(2555)  评论(1编辑  收藏  举报

导航

源码地址: https://gitee.com/zhang_jianli