C
C#16mo ago
s0wi

Using Selected Items in WPF to Filter Buses and put the Filtered Buses in a ComboBox

CmbBus should output the name of bus4 after I select matching properties of bus4 but the combo box is still empty. How do I fix this so it will show any bus that matches with the properties I selected. Let me know if you need more details. This is my BusesViewModel code: https://pastebin.com/DfnM7V7B
Pastebin
BusesViewModel.cs - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
53 Replies
Kouhai
Kouhai16mo ago
$paste
MODiX
MODiX16mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Kouhai
Kouhai16mo ago
Please paste there instead of pastebin ThumbsUpSmile
s0wi
s0wi16mo ago
BlazeBin - adgzortxniiq
A tool for sharing your source code with the world!
Kouhai
Kouhai16mo ago
Okay couple of things here First you raise OnPropertyChanged on FilteredBuses, but FilteredBuses isn't actually changed Second, why are you making the Buses prop an ObservableCollection if you're not binding to it?
s0wi
s0wi16mo ago
Sorry I’m new and I’m still not sure how data binding works I took some of the code in chatgpt😢 sorry Ill just remove filtered buses? is it not necessary to have it there?
Kouhai
Kouhai16mo ago
Can you show your xaml file as well 😅 ?
s0wi
s0wi16mo ago
Yesyes
s0wi
s0wi16mo ago
BlazeBin - ukvztomknjtm
A tool for sharing your source code with the world!
s0wi
s0wi16mo ago
My Bus class if you need it🙌 https://paste.mod.gg/fmwawkkkfvna/2
BlazeBin - fmwawkkkfvna
A tool for sharing your source code with the world!
Kouhai
Kouhai16mo ago
Sorry was away, lemme check them real quick Alright, you need to change the logic to something like this
public Time SelectedTime
{
get => _selectedTime;
set
{
_selectedTime = value;
matchingBuses = MatchingBuses()
OnPropertyChanged(nameof(FilteredBuses));
}
}
public Time SelectedTime
{
get => _selectedTime;
set
{
_selectedTime = value;
matchingBuses = MatchingBuses()
OnPropertyChanged(nameof(FilteredBuses));
}
}
s0wi
s0wi16mo ago
Will this be the same for the others? I'll try this right now
s0wi
s0wi16mo ago
s0wi
s0wi16mo ago
still empty?
Kouhai
Kouhai16mo ago
Gimme a second to take a look at the code again 😅 OH Change MatchingBuses to a method and change
- public IEnumerable<Bus> FilteredBuses => MatchingBuses;
+ public IEnumerable<Bus> FilteredBuses => matchingBuses;
- public IEnumerable<Bus> FilteredBuses => MatchingBuses;
+ public IEnumerable<Bus> FilteredBuses => matchingBuses;
s0wi
s0wi16mo ago
Kouhai
Kouhai16mo ago
Yeah
s0wi
s0wi16mo ago
its still empty 😦
s0wi
s0wi16mo ago
is my selected items not matching? or the filter method is not working TT
Kouhai
Kouhai16mo ago
Are you using CommunityToolkit.MVVM?
s0wi
s0wi16mo ago
i dont remember downloading that
Kouhai
Kouhai16mo ago
Okay go over to your BusesViewModel and hover over ObservableObject next to it Under what namespace is it? Also Are you 100% sure you're binding to your ViewModel?
s0wi
s0wi16mo ago
DigiTrip_App.Core.ObservableObject
s0wi
s0wi16mo ago
not suree
Kouhai
Kouhai16mo ago
Okay, F12 over it
s0wi
s0wi16mo ago
Kouhai
Kouhai16mo ago
@s0wi When you run the program does an icon like this show up on the top
s0wi
s0wi16mo ago
Where exactly? i dont see any symbols like that
Kouhai
Kouhai16mo ago
s0wi
s0wi16mo ago
Kouhai
Kouhai16mo ago
The program's window
s0wi
s0wi16mo ago
Kouhai
Kouhai16mo ago
Okay, can you select a date and check if it changes to red?
s0wi
s0wi16mo ago
omg yes
s0wi
s0wi16mo ago
also time i was so confused why it turns red earlier 🫠 What does this mean tho? should I convert something?
Kouhai
Kouhai16mo ago
No you don't need a converter You can simply do
DisplayMemberPath="Departure"
SelectedValuePath="Departure"
SelectedValue="{Binding SelectedTime, Mode=TwoWay}"
DisplayMemberPath="Departure"
SelectedValuePath="Departure"
SelectedValue="{Binding SelectedTime, Mode=TwoWay}"
Don't bind SelectedItem, bind to SelectedValue on your item source's elemenets
s0wi
s0wi16mo ago
Okk Why not selectedItem?
Kouhai
Kouhai16mo ago
Do this for the other control as well SelectedItems WILL force you to make a converter
s0wi
s0wi16mo ago
i do this to Date also? not from and to?
Kouhai
Kouhai16mo ago
Because SelectedItems means bind an item from the ItemSource to a property, your item source has Bus objects in them, it can't bind to DateTime or Time
s0wi
s0wi16mo ago
Ohhh
Kouhai
Kouhai16mo ago
From and To bind to their respective types
s0wi
s0wi16mo ago
Ill change them to Selected Value also?
Kouhai
Kouhai16mo ago
No need, the properties are already of type BusTerminal
s0wi
s0wi16mo ago
THANK YOU SO MUCH TVT THIS REALLY HELPED
s0wi
s0wi16mo ago
Wait is it important that I put selected value path?
Kouhai
Kouhai16mo ago
It's important, without it the binding will fail
s0wi
s0wi16mo ago
Are you busy after this? I might add a question here later TvT
Kouhai
Kouhai16mo ago
I'd probably be busy yeah, you can open another thread or head over at #gui Some amazing people are very active ThumbsUpSmile
s0wi
s0wi16mo ago
THANK YOU SO MUCH
Kouhai
Kouhai16mo ago
np
MODiX
MODiX16mo ago
s0wi#3448
Please help me, I'm confused why it doesn't show up when my other View like cancellation shows up just fine. Can someone help me so that it would show once I click on the Travel History? I would really appreciate who helps. Tell me if you want more details. For now this is the link of the usercontrol and mainwindow.xaml: https://paste.mod.gg/aialvcampztg/0
Embed Type
Link
From s0wi#3448
React with ❌ to remove this embed.