C
C#2y 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
Kouhai2y ago
$paste
MODiX
MODiX2y 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
Kouhai2y ago
Please paste there instead of pastebin ThumbsUpSmile
s0wi
s0wiOP2y ago
BlazeBin - adgzortxniiq
A tool for sharing your source code with the world!
Kouhai
Kouhai2y 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
s0wiOP2y 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
Kouhai2y ago
Can you show your xaml file as well 😅 ?
s0wi
s0wiOP2y ago
Yesyes
s0wi
s0wiOP2y ago
BlazeBin - ukvztomknjtm
A tool for sharing your source code with the world!
s0wi
s0wiOP2y 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
Kouhai2y 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
s0wiOP2y ago
Will this be the same for the others? I'll try this right now
s0wi
s0wiOP2y ago
s0wi
s0wiOP2y ago
still empty?
Kouhai
Kouhai2y 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
s0wiOP2y ago
Kouhai
Kouhai2y ago
Yeah
s0wi
s0wiOP2y ago
its still empty 😦
s0wi
s0wiOP2y ago
is my selected items not matching? or the filter method is not working TT
Kouhai
Kouhai2y ago
Are you using CommunityToolkit.MVVM?
s0wi
s0wiOP2y ago
i dont remember downloading that
Kouhai
Kouhai2y 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
s0wiOP2y ago
DigiTrip_App.Core.ObservableObject
s0wi
s0wiOP2y ago
not suree
Kouhai
Kouhai2y ago
Okay, F12 over it
s0wi
s0wiOP2y ago
Kouhai
Kouhai2y ago
@s0wi When you run the program does an icon like this show up on the top
s0wi
s0wiOP2y ago
Where exactly? i dont see any symbols like that
Kouhai
Kouhai2y ago
s0wi
s0wiOP2y ago
Kouhai
Kouhai2y ago
The program's window
s0wi
s0wiOP2y ago
Kouhai
Kouhai2y ago
Okay, can you select a date and check if it changes to red?
s0wi
s0wiOP2y ago
omg yes
s0wi
s0wiOP2y ago
also time i was so confused why it turns red earlier 🫠 What does this mean tho? should I convert something?
Kouhai
Kouhai2y 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
s0wiOP2y ago
Okk Why not selectedItem?
Kouhai
Kouhai2y ago
Do this for the other control as well SelectedItems WILL force you to make a converter
s0wi
s0wiOP2y ago
i do this to Date also? not from and to?
Kouhai
Kouhai2y 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
s0wiOP2y ago
Ohhh
Kouhai
Kouhai2y ago
From and To bind to their respective types
s0wi
s0wiOP2y ago
Ill change them to Selected Value also?
Kouhai
Kouhai2y ago
No need, the properties are already of type BusTerminal
s0wi
s0wiOP2y ago
THANK YOU SO MUCH TVT THIS REALLY HELPED
s0wi
s0wiOP2y ago
Wait is it important that I put selected value path?
Kouhai
Kouhai2y ago
It's important, without it the binding will fail
s0wi
s0wiOP2y ago
Are you busy after this? I might add a question here later TvT
Kouhai
Kouhai2y 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
s0wiOP2y ago
THANK YOU SO MUCH
Kouhai
Kouhai2y ago
np
MODiX
MODiX2y 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.
Want results from more Discord servers?
Add your server