I am Using MVVM Pattern.
when I select a Country from the listbox1 then second listbox
will display states from the selected country.
ListBox For Country
<pmControls:pmListBox x:Name="countryListBox" Grid.Row="1" Margin="3" ItemsSource="{Binding Countries}" SelectedItem="{Binding SelectedCountry,Mode=TwoWay}" >
<pmControls:pmListBox.ItemTemplate >
<DataTemplate >
<Button Command="{Binding DataContext.GetAllStatesCommand,ElementName=countryListBox}" Margin="3" Width="100" Height="25" Content="{Binding Title}" >
</Button>
</DataTemplate>
</pmControls:pmListBox.ItemTemplate>
</pmControls:pmListBox>
ListBox For State
<pmControls:pmListBox x:Name="stateListBox" Grid.Row="1" Margin="3" ItemsSource="{Binding States}">
<pmControls:pmListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding StateTitle}" ></TextBlock>
</DataTemplate>
</pmControls:pmListBox.ItemTemplate>
</pmControls:pmListBox>
In ModelView these are my commands:
command to get all Countries:
public void getCountries()
{
CountryServiceClient client = new CountryServiceClient();
client.GetCountryDetailsCompleted += (clientS, eventE) =>
{
if (eventE.Error == null)
{
foreach (var item in eventE.Result)
{
countries.Add(item);
}
}
};
client.GetCountryDetailsAsync();
}
command to get all states for selected Country:
public void ExecutegetAllStatesCommand(EventToCommandArgs args)
{
selectedState = new States();
states = new ObservableCollection<States>();
int cntry_id = this.SelectedCountry.Country_Id;
StateServiceClient client = new StateServiceClient();
client.GetStatesCompleted += (clientS, eventE) =>
{
if (eventE.Error == null)
{
foreach (var item in eventE.Result)
{
states.Add(item);
}
}
};
client.GetStatesAsync(cntry_id);
}
Here i got the data correctly in my list states but it doesnt appears on Xaml.
Please help.
You mentioned that the ‘states’ are being set correctly in the viewmodel, in that case the problem could be one of the following:
INotifyPropertyChangedfor the viewmodel and call the propertychanged event handlerCollectionChangedof theObservableCollectionthat you are adding States to as the propertychanged will not automatically trigger when adding items.