In the code below, I have a list of states and once the user selects a state, all the cities of the state appear in the second combo box.
// xaml code:
SelectionChanged="StateComboBox_SelectionChanged" Width="75" />
Width="150" />
Text="Loading Cities..." FontSize="15" />
// code-behind
public partial class MainPage : UserControl
{
// add Service Reference:
// http://www.webservicex.net/uszip.asmx
// to the SL project
public MainPage()
{
InitializeComponent();
Loaded += MainPage_Loaded;
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
MessageBlock.Visibility = Visibility.Collapsed;
StateComboBox.Items.Add("AZ");
StateComboBox.Items.Add("CA");
StateComboBox.Items.Add("FL");
StateComboBox.Items.Add("HI");
}
private void StateComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBlock.Visibility = Visibility.Visible;
CityComboBox.Items.Clear();
USZipSoapClient zipSoapClient = new USZipSoapClient();
zipSoapClient.GetInfoByStateCompleted += ZipSoapClient_GetInfoByStateCompleted;
string stateCode = StateComboBox.SelectedItem.ToString();
zipSoapClient.GetInfoByStateAsync(stateCode);
}
void ZipSoapClient_GetInfoByStateCompleted(object sender, GetInfoByStateCompletedEventArgs e)
{
string xmlResult = e.Result.ToString();
XmlReader resultXml = XmlReader.Create(new StringReader(xmlResult));
while (resultXml.Read())
{
if (resultXml.NodeType != XmlNodeType.Element) continue;
switch (resultXml.LocalName.ToLower())
{
case "city":
string cityName = resultXml.ReadInnerXml();
if(!CityComboBox.Items.Contains(cityName))
{
CityComboBox.Items.Add(cityName);
}
break;
}
}
MessageBlock.Visibility = Visibility.Collapsed;
}
}