Help sort the data in listbox.

Find the TextBlock Text element of which will be equal to the selected Combobox item, then delete the extra ListBoxItems and leave only those that contain the selected Combobox item

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { string text = (sender as ComboBox).SelectedItem as string; foreach (var realmBlock in FindVisualChildren<TextBlock>(this)) { if (realmBlock.Text == text) { ArrayList arList = new ArrayList(); foreach (object obj in charList.Items) { arList.Add(obj); } //arList.Sort(); charList.Items.Clear(); foreach (object obj in arList) { charList.Items.Add(obj); } } } } 

Do not quite understand how to do it

Listbox code

 <ListBox x:Name="charList" Width="326" HorizontalAlignment="Left" Background="{x:Null}" BorderBrush="Transparent" ItemContainerStyle="{StaticResource ListBoxItem}" ItemTemplate="{DynamicResource CharTemplate}" ItemsSource="{Binding}" SelectedIndex="0" Padding="0,5,0,0" VirtualizingPanel.ScrollUnit="Pixel"> <ListBox.Resources> <DataTemplate x:Key="CharTemplate"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="125" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Image x:Name="listCharPortait" Width="80" Height="50" Source="{Binding ImageLink}" Stretch="Fill" /> <StackPanel Grid.Column="1" MaxWidth="174" MaxHeight="100" Margin="-5,25,0,25" HorizontalAlignment="Left" VerticalAlignment="Top"> <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Horizontal" TextBlock.FontWeight="Bold"> <TextBlock x:Name="nameBlock" Text="{Binding Realms}" TextTrimming="CharacterEllipsis" TextWrapping="Wrap"> <TextBlock.Foreground> <SolidColorBrush Opacity="0.5" Color="#FF959595" /> </TextBlock.Foreground> </TextBlock> </StackPanel> <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Horizontal"> <TextBlock FontFamily="/Launcher;component/Resources/#Blizzard" FontSize="14" Foreground="White" Text="{Binding Title, FallbackValue=Title}" TextTrimming="CharacterEllipsis" TextWrapping="Wrap" /> </StackPanel> </StackPanel> </Grid> </DataTemplate> </ListBox.Resources> </ListBox> 
  • @VladD do not look? - Elizabeth
  • It is not entirely clear how your visual elements are located. Do you have a TextBlock nested in a ComboBoxItem? Is a listbox a separate item with a separate collection? So or not? - Bulson
  • Updated the question - Elizabeth
  • Do you really need it urgently? Or you can wait until the morning? - Bulson
  • As to you, the second day I’m fighting with this, if I don’t get to me by morning, I’d be glad to hear from you - Elizabeth

1 answer 1

In a good way would be better if you used the board. Then everything would be much easier. Two properties of type ObservableCollection<T> for ComboBox and ListBox . And for the SelectedItem property CurrentElement . When selecting an item, it would be possible to use LINQ on the collection property for a ListBox .

  • Could you give a code, I don’t quite understand what you mean - Elizabeth
  • Here you can download: yadi.sk/d/eEOKgoCXwoM8g - Bulson
  • Check out this version, where I’ve done with the ViewModel and with the commands yadi.sk/d/ud7mV3Fbwqr6G - Bulson
  • well thank you! I will come home, I will try - Elizaveta
  • Unfortunately I could not figure out your version, I have differences, and I just can’t do it by example (( - Elizabeth