none
如何将TreeView绑定到“Application”中的一个“ObservableCollection<> “类型变量中? RRS feed

  • 问题

  • 各位大师:

    在Application 中定义了一个”ObservableCollection<Person>“类型的变量,姑且称为”Companys“,并且在其填充了数据,在Window中有一个”TreeView“类型的控件需要绑定这个”Companys“变量,如何实现呢?

    类Application是这样定义的:

     public partial class App : Application
     {
      public ObservableCollection<Company> Companys { get; set; }

      public App()
      {
       this.Companys = new ObservableCollection<Company>();
      }

      private void Application_Startup(object sender, StartupEventArgs e)
      {
       //information for Company-1
       Company dataCompany1 = new Company() { Name = "北京XX公司", Description = "系统销售商" };
       this.Companys.Add(dataCompany1);
        Department dataDepartment11 = new Department() { Name = "工程部", Description = "负责施工和售后服务的部门" };
        dataCompany1.Departments.Add(dataDepartment11);
        dataDepartment11.Persons.Add(new Person() { Name = "张三", Description = "经理" });
        dataDepartment11.Persons.Add(new Person() { Name = "李四", Description = "工程师" });
        dataDepartment11.Persons.Add(new Person() { Name = "王五", Description = "工程师" });
        Department dataDepartment12 = new Department() { Name = "开发部", Description = "负责产品开发的部门" };
        dataCompany1.Departments.Add(dataDepartment12);
        dataDepartment12.Persons.Add(new Person() { Name = "张三", Description = "经理" });
        dataDepartment12.Persons.Add(new Person() { Name = "李四", Description = "工程师" });
        dataDepartment12.Persons.Add(new Person() { Name = "王五", Description = "工程师" });
        Department dataDepartment13 = new Department() { Name = "产品部", Description = "负责产品整合的部门" };
        dataCompany1.Departments.Add(dataDepartment13);
        dataDepartment13.Persons.Add(new Person() { Name = "张三", Description = "经理" });
        dataDepartment13.Persons.Add(new Person() { Name = "李四", Description = "工程师" });
        dataDepartment13.Persons.Add(new Person() { Name = "王五", Description = "工程师" });

       //information for Company-2
       Company dataCompany2 = new Company() { Name = "北京YY公司", Description = "系统应用整合商" };
       this.Companys.Add(dataCompany2);
        Department dataDepartment21 = new Department() { Name = "工程部", Description = "负责施工和售后服务的部门" };
        dataCompany2.Departments.Add(dataDepartment21);
        dataDepartment21.Persons.Add(new Person() { Name = "张三", Description = "经理" });
        dataDepartment21.Persons.Add(new Person() { Name = "李四", Description = "工程师" });
        dataDepartment21.Persons.Add(new Person() { Name = "王五", Description = "工程师" });
        Department dataDepartment22 = new Department() { Name = "开发部", Description = "负责产品开发的部门" };
        dataCompany2.Departments.Add(dataDepartment22);
        dataDepartment22.Persons.Add(new Person() { Name = "张三", Description = "经理" });
        dataDepartment22.Persons.Add(new Person() { Name = "李四", Description = "工程师" });
        dataDepartment22.Persons.Add(new Person() { Name = "王五", Description = "工程师" });
        Department dataDepartment23 = new Department() { Name = "产品部", Description = "负责产品整合的部门" };
        dataCompany2.Departments.Add(dataDepartment23);
        dataDepartment23.Persons.Add(new Person() { Name = "张三", Description = "经理" });
        dataDepartment23.Persons.Add(new Person() { Name = "李四", Description = "工程师" });
        dataDepartment23.Persons.Add(new Person() { Name = "王五", Description = "工程师" });
      }

      public ObservableCollection<Company> GetCompanys()
      {
       //return the list for all companys
       return this.Companys;
      }
     }

    Window所对应的”XAML“文件是这样定义的:

    <Window x:Class="Sample.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:Sample="clr-namespace:Sample"
     Title="Window1" Height="500" Width="300" Background="Gray" Loaded="Window_Loaded">
     
        <Grid>
      <Grid.Resources>
       <ObjectDataProvider x:Key="allCompanys" ObjectInstance="{x:Type Sample:App}" MethodName="GetCompanys"/>
      </Grid.Resources>
      <Grid.RowDefinitions>
       <RowDefinition Height="30"/>
       <RowDefinition/>
      </Grid.RowDefinitions>
      <Label Grid.Row="0" Margin="2" Content="示范数据:" VerticalContentAlignment="Bottom"/>
      
      <TreeView Grid.Row="1" Name="tvCompany" Margin="2" Background="DarkBlue" ItemsSource="{Binding Source={StaticResource allCompanys}}">
       <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Path=Departments}">
         <TextBlock Text="{Binding Path=Name}"/>
         <HierarchicalDataTemplate.ItemTemplate>
          <HierarchicalDataTemplate ItemsSource="{Binding Path=Persons}">
           <TextBlock Text="{Binding Path=Name}"/>
           <HierarchicalDataTemplate.ItemTemplate>
            <DataTemplate>
             <TextBlock Text="{Binding Path=Name}"/>
            </DataTemplate>
           </HierarchicalDataTemplate.ItemTemplate>
          </HierarchicalDataTemplate>
         </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
       </TreeView.ItemTemplate>
      </TreeView>
     </Grid>
    </Window>

    在上面的代码中,类Company, Department以及Person都是彼此嵌套的关系,每个类中只有两个变量,即"Name"和”Description“。

    但是结果就是不对,在TreeView中没有任何数据显示。

    是什么地方不对呢?渴望那位大仙给指点一下,郁闷死了!

    先谢了。

     


    一派胡言
    2011年11月17日 11:29

答案

  • 呵呵,问题已经解决了。

    有两种方法,共其他由此困惑的朋友参看。注意代码中“粗体”文字的部分:

    方法一:

    使用“ObjectDataProvider”。

    <Window x:Class="Sample.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:Sample="clr-namespace:Sample"
     Title="Window1" Height="500" Width="300" Background="Gray" Loaded="Window_Loaded">
     
        <Grid>
      <Grid.Resources>
       <ObjectDataProvider x:Key="allCompanys"  ObjectInstance="{x:Static Application.Current}" MethodName="GetCompanys"/>
      </Grid.Resources>
      <Grid.RowDefinitions>
       <RowDefinition Height="30"/>
       <RowDefinition/>
      </Grid.RowDefinitions>
      <Label Grid.Row="0" Margin="2" Content="示范数据:" VerticalContentAlignment="Bottom"/>
      
      <TreeView Grid.Row="1" Name="tvCompany" Margin="2" Background="DarkBlue" ItemsSource="{Binding Source={StaticResource allCompanys}}">

    其余代码省略
     </Grid>
    </Window>

    方法二:

    直接编写绑定代码。

    <Window x:Class="Sample.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:Sample="clr-namespace:Sample"
     Title="Window1" Height="500" Width="300" Background="Gray" Loaded="Window_Loaded">
     
        <Grid>
      <Grid.RowDefinitions>
       <RowDefinition Height="30"/>
       <RowDefinition/>
      </Grid.RowDefinitions>
      <Label Grid.Row="0" Margin="2" Content="示范数据:" VerticalContentAlignment="Bottom"/>
      
      <TreeView Grid.Row="1" Name="tvCompany" Margin="2" Background="DarkBlue" ItemsSource="{Binding Source={x:Static Application.Current}, Path=Companys}">

    其余代码省略

     </Window>

    希望对大家有帮助。

    谢谢各位的关注。

     


    一派胡言
    2011年11月18日 3:48

全部回复

  • 都过去了十几个小时了,居然没有任何反馈。是这个问题太难,还是大师们都休息去了?

    自己顶一下!依旧郁闷中。

     


    一派胡言
    2011年11月18日 3:18
  • 呵呵,问题已经解决了。

    有两种方法,共其他由此困惑的朋友参看。注意代码中“粗体”文字的部分:

    方法一:

    使用“ObjectDataProvider”。

    <Window x:Class="Sample.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:Sample="clr-namespace:Sample"
     Title="Window1" Height="500" Width="300" Background="Gray" Loaded="Window_Loaded">
     
        <Grid>
      <Grid.Resources>
       <ObjectDataProvider x:Key="allCompanys"  ObjectInstance="{x:Static Application.Current}" MethodName="GetCompanys"/>
      </Grid.Resources>
      <Grid.RowDefinitions>
       <RowDefinition Height="30"/>
       <RowDefinition/>
      </Grid.RowDefinitions>
      <Label Grid.Row="0" Margin="2" Content="示范数据:" VerticalContentAlignment="Bottom"/>
      
      <TreeView Grid.Row="1" Name="tvCompany" Margin="2" Background="DarkBlue" ItemsSource="{Binding Source={StaticResource allCompanys}}">

    其余代码省略
     </Grid>
    </Window>

    方法二:

    直接编写绑定代码。

    <Window x:Class="Sample.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:Sample="clr-namespace:Sample"
     Title="Window1" Height="500" Width="300" Background="Gray" Loaded="Window_Loaded">
     
        <Grid>
      <Grid.RowDefinitions>
       <RowDefinition Height="30"/>
       <RowDefinition/>
      </Grid.RowDefinitions>
      <Label Grid.Row="0" Margin="2" Content="示范数据:" VerticalContentAlignment="Bottom"/>
      
      <TreeView Grid.Row="1" Name="tvCompany" Margin="2" Background="DarkBlue" ItemsSource="{Binding Source={x:Static Application.Current}, Path=Companys}">

    其余代码省略

     </Window>

    希望对大家有帮助。

    谢谢各位的关注。

     


    一派胡言
    2011年11月18日 3:48
  • 还没来得及给你解答,已经找到了,不错兄弟。我们一般会在24到48小时给出回复,请谅解论坛是有一定延时的。赞一个,谢谢分享。


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2011年11月18日 5:10
    版主
  • 其实我个人推荐你里面的第二种方式,不需要额外的一个方法来返回这个集合。
    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    2011年11月18日 5:12
    版主
  • 谢谢斑竹的关注!这个地方就是一个大家互相帮助的天地,在接受别人帮助的同时,如果也能帮助到别人,是一件非常愉快的事情。顺祝各位朋友周末愉快!


    一派胡言
    2011年11月18日 9:41