在silverlight中,在2个combox中建立关联该怎样?求指导
如图,点击第一个combox中的一项的“省份”,在第二个中自动显示“城市”的列表我的数据库表是,用id去关联省份和城市的关系。本人初手,希望各位能给出源代码。...
如图,点击第一个combox中的一项的“省份”,在第二个中自动显示“城市”的列表
我的数据库表是,用id去关联省份和城市的关系。本人初手,希望各位能给出源代码。 展开
我的数据库表是,用id去关联省份和城市的关系。本人初手,希望各位能给出源代码。 展开
2个回答
展开全部
写了段demo给你,看下应该就懂了。
xaml:
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
<ComboBox DisplayMemberPath="CountryId" SelectedValuePath="Cities" SelectionChanged="Conbo_father_SelectionChanged" x:Name="Conbo_father" Width="100" Height="25"/>
<ComboBox DisplayMemberPath="CityId" x:Name="Conbo_son" Width="100" Height="25" Margin="20,0,0,0"/>
</StackPanel>
后台:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
List<City> citylistA=new List<City>
{
new City(){CityId="a1"},
new City(){CityId="a2"},
new City(){CityId="a3"}
};
List<City> citylistB = new List<City>
{
new City(){CityId="b1"},
new City(){CityId="b2"},
new City(){CityId="b3"}
};
List<Country> CountryList = new List<Country>
{
new Country(){CountryId="A",Cities=citylistA},
new Country(){CountryId="B",Cities=citylistB}
};
Conbo_father.ItemsSource = CountryList;
}
private void Conbo_father_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Conbo_son.ItemsSource = Conbo_father.SelectedValue as List<City>;
}
}
public class Country
{
public string CountryId { get; set; }
public List<City> Cities { get; set; }
}
public class City
{
public string CityId { get; set; }
xaml:
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
<ComboBox DisplayMemberPath="CountryId" SelectedValuePath="Cities" SelectionChanged="Conbo_father_SelectionChanged" x:Name="Conbo_father" Width="100" Height="25"/>
<ComboBox DisplayMemberPath="CityId" x:Name="Conbo_son" Width="100" Height="25" Margin="20,0,0,0"/>
</StackPanel>
后台:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
List<City> citylistA=new List<City>
{
new City(){CityId="a1"},
new City(){CityId="a2"},
new City(){CityId="a3"}
};
List<City> citylistB = new List<City>
{
new City(){CityId="b1"},
new City(){CityId="b2"},
new City(){CityId="b3"}
};
List<Country> CountryList = new List<Country>
{
new Country(){CountryId="A",Cities=citylistA},
new Country(){CountryId="B",Cities=citylistB}
};
Conbo_father.ItemsSource = CountryList;
}
private void Conbo_father_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Conbo_son.ItemsSource = Conbo_father.SelectedValue as List<City>;
}
}
public class Country
{
public string CountryId { get; set; }
public List<City> Cities { get; set; }
}
public class City
{
public string CityId { get; set; }
追问
我用wcf ria server 去连接数据库了,不需要写city和country数据结构了吧?
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询