Search code examples
silverlightwindows-phone-7silverlight-toolkit

Update ListPicker on SelectionChanged


I have two list pickers. When first list picker have selection changed it should filter data of second list picker. Second ListPicker xaml is like this

 <TextBlock HorizontalAlignment="Left" Style="{StaticResource PhoneTextNormalStyle}" TextWrapping="Wrap" Text="text" VerticalAlignment="Bottom"/>
                <toolkit:ListPicker VerticalAlignment="Bottom" Name="CategoryList" ItemsSource="{Binding TransactionCategories, Mode=TwoWay}" SelectedItem="{Binding SelectedTransactionCategory, Mode=TwoWay}">
                <toolkit:ListPicker.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Rectangle Fill="Red" Width="24" Height="24"/>
                            <TextBlock Text="{Binding CategoryName,Mode=TwoWay}" Margin="12 0 0 0"/>
                        </StackPanel>
                    </DataTemplate>
                </toolkit:ListPicker.ItemTemplate>
                <toolkit:ListPicker.FullModeItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="16 21 0 20">
                            <Rectangle Width="43" Height="43"/>
                            <TextBlock Text="{Binding CategoryName, Mode=TwoWay}" Margin="16 0 0 0" FontSize="43" FontFamily="{StaticResource PhoneFontFamilyLight}" TextWrapping="Wrap"/>
                        </StackPanel>
                    </DataTemplate>
                </toolkit:ListPicker.FullModeItemTemplate>
            </toolkit:ListPicker>

First picker fires the event GroupCategoryList_SelectionChanged

 private void GroupCategoryList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var category = (TransactionGroupCategory) GroupCategoryList.SelectedItem;
        ((TransactionEditViewModel)DataContext).FilterCategoryByGroup(category.GroupCategoryId);
    }

This is a metod in ViewModel

 public  void FilterCategoryByGroup(int groupCategoryId)
    {

        TransactionCategories = ToObservableCollection(DatabaseBl.GetData<TransactionCategory>().Where(x => x.GroupCategoryId == groupCategoryId).OrderByDescending(tc => tc.TransactionCount));
        if (TransactionCategories.Count > 0)
            SelectedTransactionCategory = TransactionCategories[0];
    }

The second list picker never get refreshed in UI, even though new data was fetched through filter method. What could be wrong with this. Binding issue maybe?


Solution

  • Without looking at the rest of the view model code, the usual suspect here is that you're replacing the observable collection on the view model rather than modifying it. Unless your view model is firing a NotifyPropertyChanged event when you set the TransactionCategories then the view will not know of the changes.

    The better approach IMO for this is not to replace the entire collection but to modify it, possibly calling Clear and Add to refill the collection. The collection will then fire Collection Changed events which the view will notice.