I have a listbox that is filtered as a user types into a listbox. The selection changed event works when a user selects an item but does not work correctly when the filter is being engaged. If I have used the filter to narrow down my listbox to one result and then click that result, the selected item property points to the first item in the list. Why is this happening and is there a way around this?
<ListBox Name="lstTiers" Margin="10,0,10,0" SelectionChanged="ListBox_SelectionChanged_2"><ListBox.ItemTemplate><DataTemplate><Border Margin="2" Background="WhiteSmoke"><Grid><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Text="{Binding Path=Name}" FontWeight="Bold"/><TextBlock Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" FontStyle="Italic" Foreground="Gray" Text="{Binding Path=Type}" Margin="5,0,0,0" TextWrapping="Wrap"/><TextBlock Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Blue" Text="{Binding Path=Tier, StringFormat=Tier {0}}" Margin="5,0,0,0" TextWrapping="Wrap"/></Grid></Border></DataTemplate></ListBox.ItemTemplate></ListBox>
cvs =(CollectionViewSource)this.FindResource("TierCvs"); this.dataSource = Objects.User.FetchAllUsersAndParents(); cvs.Source = dataSource; Binding b = new Binding(); b.Source = cvs; try { BindingOperations.SetBinding(lstTiers, ListView.ItemsSourceProperty, b); } catch(Exception e) { }
private void ListBox_SelectionChanged_2(object sender, SelectionChangedEventArgs e) { if (lstTiers.SelectedIndex != -1 & PageInit == false) { cvs2.Source = dataSource[lstTiers.SelectedIndex].Users; //This index always is 0 Binding b = new Binding(); b.Source = cvs2; BindingOperations.SetBinding(lstUsers, ListView.ItemsSourceProperty, b); } }