I have a listbox called lstTickets and a combobox called cboFilter. I am trying to filter the items in my listbox based on the ticket.District that is in the combobox. So for example if I select from the combox, is the only team in the listbox. Any help is appreciated.
List<Ticket> = new List<Ticket>()
{
new Ticket { Name = "Arsenal", Badge = "images/arsenal.jpg", Price = 900, Stadium = "emirates.jpg", District = "London" },
new Ticket { Name = "Tottenham Hotspur", Badge = "images/tottenham.jpg", Price = 850, Stadium = "tottenham.jpg", District = "" },
new Ticket { Name = "Manchester United", Badge = "images/.png", Price = 1000, Stadium = "oldtrafford.jpg", District = "" },
};
private void cboFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var query = from Ticket in tickets where cboFilter.SelectedItem.ToString() == Ticket.District select Ticket;
lstTickets.ItemsSource = null;
lstTickets.Items.Clear();
List<Ticket> tmp = new List<Ticket>();
foreach (var tickets in query)
{
tmp.Add(tickets);
}
lstTickets.ItemsSource = tmp;
}
<Image Width="200" Height="150" HorizontalAlignment="Left" Source="{x:Bind Badge }" />
<TextBlock FontSize="22" HorizontalAlignment="Left" Text="{x:Bind Name}" Style="{StaticResource HeaderTextBlockStyle}"/>
<TextBlock FontSize="16" HorizontalAlignment="Left"> € <Run Text="{Binding Price}" /></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Edit: Populating combobox in mainpage.cs
private void myGrid_Loaded(object sender, RoutedEventArgs e)
{
}
looks like the ToString() doen't work property for the selected item.
var searchTerm= cboFilter.SelectedItem.ToString();
var query = from Ticket in tickets where Ticket.District == term select Ticket;
make sure you put a break point at search term to see if its the correct valueto get
but i think i know your issue, the selected item will return the whole object so do the following
var selectedTicked= cboFilter.SelectedItem as Ticket;
var searchTerm= selectedTicket.District
var query = from Ticket in tickets where Ticket.District == term select Ticket;
Try checking that first you have an actual value in the cboFilter.SelectedItem and then if you do comparing the values using lower case. I prefer to use System.Linq, so I've altered your query to use that:
private void cboFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var comboBoxItem = cboFilter.SelectedItem as ComboBoxItem;
if(comboBoxItem == null) return; // or log an error here or something if it should not be empty.
var query = tickets.Where(t => t.District.ToLower() == comboBoxItem.Content.ToString().ToLower());
lstTickets.ItemsSource = null;
lstTickets.Items.Clear();
List<Ticket> tmp = new List<Ticket>();
foreach (var tickets in query)
{
tmp.Add(tickets);
}
lstTickets.ItemsSource = tmp;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.