展开全部
xaml:
<StackPanel Orientation="Horizontal">
<ComboBox Name="cb1" ItemsSource="{Binding}" Width="200" Height="30" SelectionChanged="Cb1_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ComboBox Name="cb2" ItemsSource="{Binding}" Width="200" Height="30">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
xaml.cs:
public MainWindow() //构造函数
{
InitializeComponent();
var provinces = new List<Province>
{
new Province
{
Name="山西省",
Cities = new List<City>
{
new City{Name="太原市"},new City{Name="大同市"},new City{Name="运城市"},new City{Name="榆次市"}
}
},
new Province
{
Name="陕西省",
Cities = new List<City>
{
new City{Name="西安市"},new City{Name="宝鸡市 "},new City{Name="渭南市 "},new City{Name="咸阳市 "}
}
}
};
cb1.DataContext = provinces;
}
private void Cb1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var combobox = sender as ComboBox;
if (combobox != null)
{
var province = combobox.SelectedItem as Province;
cb2.DataContext = province.Cities;
cb2.SelectedIndex = 0;
}
}
}
辅助类:
public class Province
{
public string Name { get; set; }
public List<City> Cities { get; set; }
}
public class City
{
public string Name { get; set; }
}
<StackPanel Orientation="Horizontal">
<ComboBox Name="cb1" ItemsSource="{Binding}" Width="200" Height="30" SelectionChanged="Cb1_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ComboBox Name="cb2" ItemsSource="{Binding}" Width="200" Height="30">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
xaml.cs:
public MainWindow() //构造函数
{
InitializeComponent();
var provinces = new List<Province>
{
new Province
{
Name="山西省",
Cities = new List<City>
{
new City{Name="太原市"},new City{Name="大同市"},new City{Name="运城市"},new City{Name="榆次市"}
}
},
new Province
{
Name="陕西省",
Cities = new List<City>
{
new City{Name="西安市"},new City{Name="宝鸡市 "},new City{Name="渭南市 "},new City{Name="咸阳市 "}
}
}
};
cb1.DataContext = provinces;
}
private void Cb1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var combobox = sender as ComboBox;
if (combobox != null)
{
var province = combobox.SelectedItem as Province;
cb2.DataContext = province.Cities;
cb2.SelectedIndex = 0;
}
}
}
辅助类:
public class Province
{
public string Name { get; set; }
public List<City> Cities { get; set; }
}
public class City
{
public string Name { get; set; }
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-02-24
展开全部
dgfhdghdghdhgdghdhfdghdghdghdfghdfghdfghdfhgdfghdfghdghgfhhgfdhfdhgdfghdfghdfghdghfdghfdghdfghfdgdhdfghdfghfdghdfghfgdhdfhfdghfddfghfdghdfghdfghdfghdfhfdghfgdfghfghfghfghfghfgfghfggfdgfhfdhdghfdhfdhfhfghfghfhfghfhfhfhfdfdhdhfdhghgfhfdhgfdghfghfghfdghfghfdh
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询