none
在使用WCF实现省市区联动时发现的问题,相当纠结,求高人解决? RRS feed

  • 问题

  • 首先,数据库的设计:

     Province表:ProvinceID,ProvinceName

    City表:CityID,CityName,ProvinceID

    Area表:AreaID,AreaName,CityID

     

    XAML代码:

       <ComboBox x:Name="cmbProvince" Grid.Column="1" Margin="38,0,35,0" Grid.Row="3" Grid.ColumnSpan="2" d:LayoutOverrides="Height" VerticalAlignment="Center" IsDropDownOpen="True" ItemsSource="{Binding}" DisplayMemberPath="ProvinceName" SelectedValuePath="ProvinceID" />

       <ComboBox x:Name="cmbCity" Grid.Column="3" Margin="35,0,38,0" Grid.Row="3" Grid.ColumnSpan="2" VerticalAlignment="Center" DisplayMemberPath="CityName" SelectedValuePath="CityID"/>

      <ComboBox x:Name="cmbDepartment" Grid.Column="8" Margin="-22,0,9,0" Grid.Row="3" VerticalAlignment="Center"  DisplayMemberPath="DepartmentName" SelectedValuePath="DepartmentID" >

    CS 代码:

     void regAndCall()
    
      {
    
       rsSvcClient = new ReportService.ScmWebAreaManagerClient();
    
    
    
       //注册事件
    
       rsSvcClient.GetProvincesCompleted += new EventHandler<ReportService.GetProvincesCompletedEventArgs>(rsSvcClient_GetProvincesCompleted);
    
       rsSvcClient.GetCitysCompleted += new EventHandler<ReportService.GetCitysCompletedEventArgs>(rsSvcClient_GetCitysCompleted);
    
       rsSvcClient.GetAreasCompleted += new EventHandler<ReportService.GetAreasCompletedEventArgs>(rsSvcClient_GetAreasCompleted);
    
    
    
       this.cmbProvince.SelectionChanged += new SelectionChangedEventHandler(cmbProvince_SelectionChanged);
    
       this.cmbCity.SelectionChanged += new SelectionChangedEventHandler(cmbCity_SelectionChanged);
    
    
    
       //调用GetProvincesAsync()方法查询
    
       rsSvcClient.GetProvincesAsync(sessionID);
    
       rsSvcClient.GetCitysAsync(sessionID);
    
       rsSvcClient.GetAreasAsync(sessionID);
    
      }
    
    
    
      //处理省份
    
      void rsSvcClient_GetProvincesCompleted(object sender, ReportService.GetProvincesCompletedEventArgs e)
    
      {
    
       //调用成功,显示数据
    
       if (e.Error == null)
    
        cmbProvince.ItemsSource = e.Result;
    
      }
    
    
    
      //处理城市
    
      void rsSvcClient_GetCitysCompleted(object sender, ReportService.GetCitysCompletedEventArgs e)
    
      {
    
       //调用成功,显示数据
    
       if (e.Error == null)
    
        cmbCity.ItemsSource = e.Result;
    
      }
    
    
     private void cmbProvince_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    
      {
    
        var list = from x in cmbProvince.ItemsSource as ObservableCollection<ReportService.Province>
    
          join y in cmbCity.ItemsSource as ObservableCollection<ReportService.City>
    
          on x.ProvinceID equals y.ProvinceID
    
          where y.ProvinceID == short.Parse((this.cmbProvince.SelectedValue.ToString()))
    
          select new { y.CityName, y.CityID, y.ProvinceID };
    
    
    
       cmbCity.ItemsSource = list;
    
       cmbCity.UpdateLayout();
    
       cmbCity.DisplayMemberPath = "CityName";
    
       cmbCity.SelectedValuePath = "CityID";
    
      }

    两个问题:

    1,当第一次选择了省份后,城市的数据无法显示出来,但可以选择,全为空白的项,为什么?

    2.当第二次选择省份,会报错,应该是(加粗)部份那里的cmbCity.ItemsSource已经改变了,应该如何改进?

     


    2011年4月1日 7:21

答案

  • 试试这个。

    class

    using System;
    using System.Collections.ObjectModel;
    
    namespace MasterDetailsBinding
    {
      public class Team
      {
        public string Name { get; set; }
      }
    
      public class Division
      {
        public string Name { get; set; }
        public ObservableCollection<Team> Teams { get; set; }
      }
    
      public class League
      {
        public string Name { get; set; }
        public ObservableCollection<Division> Divisions { get; set; }
      }
    
      public class LeagueList : ObservableCollection<League>
      {
        public LeagueList()
        {
          for (int x = 1; x < 3; x++)
          {
            League theLeague = new League()
            {
              Name = "League " + x,
              Divisions = new ObservableCollection<Division>()
            };
    
            for (int y = 1; y < 4; y++)
            {
              Division theDivision = new Division()
              {
                Name = String.Format("Division {0}-{1}", x, y),
                Teams = new ObservableCollection<Team>()
              };
    
              for (int z = 1; z < 5; z++)
              {
                Team theTeam = new Team()
                {
                  Name = String.Format("Team {0}-{1}-{2}", x, y, z)
                };
                theDivision.Teams.Add(theTeam);
              }
              theLeague.Divisions.Add(theDivision);
            }
            this.Add(theLeague);
          }
        }
    
      } 
    
    }
    
    
    
    

     

     xaml

    <UserControl x:Class="MasterDetailsBinding.MainPage"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:local="clr-namespace:MasterDetailsBinding">
    
     <UserControl.Resources>
      <local:LeagueList x:Key="LeagueData"/>
      <CollectionViewSource x:Name="Leagues" Source="{StaticResource LeagueData}"/>
      <CollectionViewSource x:Name="Divisions" 
       Source="{Binding Divisions, Source={StaticResource Leagues}}"/>
     </UserControl.Resources>
    
     <StackPanel x:Name="LayoutRoot" Orientation="Horizontal" Margin="5"
      DataContext="{Binding Source={StaticResource Leagues}}">
    
      <StackPanel Margin="5">
       <TextBlock Text="All Leagues" Margin="3" FontWeight="Bold"/>
       <ListBox ItemsSource="{Binding}" DisplayMemberPath="Name"/>
      </StackPanel>
    
      <StackPanel Margin="5">
       <TextBlock Text="{Binding Name}" Margin="3" FontWeight="Bold"/>
       <ListBox ItemsSource="{Binding Source={StaticResource Divisions}}" 
        DisplayMemberPath="Name"/>
      </StackPanel>
    
      <StackPanel Margin="5" 
       DataContext="{Binding Source={StaticResource Divisions}}">
       <TextBlock Text="{Binding Name}" Margin="3" FontWeight="Bold"/>
       <ListBox ItemsSource="{Binding Teams}" DisplayMemberPath="Name"/>
      </StackPanel>
    
     </StackPanel>
    
    </UserControl>
    
    
    2011年4月6日 15:17

全部回复

  • 这种活,没有任何技术含量,只是麻烦而已。不应该拿来讨论。在实际做程序的时候,经常会碰上麻烦但没任何技术含量的东西要做。你需要耐心!!!是是一个老程序员对你的忠告。
    2011年4月1日 16:07
  • 试试这个。

    class

    using System;
    using System.Collections.ObjectModel;
    
    namespace MasterDetailsBinding
    {
      public class Team
      {
        public string Name { get; set; }
      }
    
      public class Division
      {
        public string Name { get; set; }
        public ObservableCollection<Team> Teams { get; set; }
      }
    
      public class League
      {
        public string Name { get; set; }
        public ObservableCollection<Division> Divisions { get; set; }
      }
    
      public class LeagueList : ObservableCollection<League>
      {
        public LeagueList()
        {
          for (int x = 1; x < 3; x++)
          {
            League theLeague = new League()
            {
              Name = "League " + x,
              Divisions = new ObservableCollection<Division>()
            };
    
            for (int y = 1; y < 4; y++)
            {
              Division theDivision = new Division()
              {
                Name = String.Format("Division {0}-{1}", x, y),
                Teams = new ObservableCollection<Team>()
              };
    
              for (int z = 1; z < 5; z++)
              {
                Team theTeam = new Team()
                {
                  Name = String.Format("Team {0}-{1}-{2}", x, y, z)
                };
                theDivision.Teams.Add(theTeam);
              }
              theLeague.Divisions.Add(theDivision);
            }
            this.Add(theLeague);
          }
        }
    
      } 
    
    }
    
    
    
    

     

     xaml

    <UserControl x:Class="MasterDetailsBinding.MainPage"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:local="clr-namespace:MasterDetailsBinding">
    
     <UserControl.Resources>
      <local:LeagueList x:Key="LeagueData"/>
      <CollectionViewSource x:Name="Leagues" Source="{StaticResource LeagueData}"/>
      <CollectionViewSource x:Name="Divisions" 
       Source="{Binding Divisions, Source={StaticResource Leagues}}"/>
     </UserControl.Resources>
    
     <StackPanel x:Name="LayoutRoot" Orientation="Horizontal" Margin="5"
      DataContext="{Binding Source={StaticResource Leagues}}">
    
      <StackPanel Margin="5">
       <TextBlock Text="All Leagues" Margin="3" FontWeight="Bold"/>
       <ListBox ItemsSource="{Binding}" DisplayMemberPath="Name"/>
      </StackPanel>
    
      <StackPanel Margin="5">
       <TextBlock Text="{Binding Name}" Margin="3" FontWeight="Bold"/>
       <ListBox ItemsSource="{Binding Source={StaticResource Divisions}}" 
        DisplayMemberPath="Name"/>
      </StackPanel>
    
      <StackPanel Margin="5" 
       DataContext="{Binding Source={StaticResource Divisions}}">
       <TextBlock Text="{Binding Name}" Margin="3" FontWeight="Bold"/>
       <ListBox ItemsSource="{Binding Teams}" DisplayMemberPath="Name"/>
      </StackPanel>
    
     </StackPanel>
    
    </UserControl>
    
    
    2011年4月6日 15:17