Reputation: 3
I am quite new to C sharp and all the MVVM thing, I've tried to solve my problem, but with no result after few days of searching, so I'm writing here. I have program for reserving and adding hostels. First, you can add a Hostel on one View page - it will add it to the SQLite database and then to the list of hostels. On the second View page I got formular for reserving hostels. When selecting which hostel you want to reserve I have a listview with list of the hostels. I bind the ItemSource to the List (the list of hostels) so I can see all the hostels there, but when I want to bind the SelectedItem to the NewReservation.HostelName - it won't do anything. Here's the code for the listview:
<StackPanel Orientation="Horizontal" Margin="0,0,0,0">
<TextBlock Text="Hostel name" Width="160" Margin="12,12,12,12" Height="20" Foreground="Black" FontFamily="Microsoft JhengHei Light" FontSize="18" />
<ListView Margin="10,0,0,0" Width="223" ItemsSource="{Binding List}" SelectedItem="{Binding NewReservation.hostelName, Mode=TwoWay}" Height="100" Background="#FFD3DCC1" Foreground="#FF183440" RequestedTheme="Dark" Padding="5,5,5,0">
<ListView.DataContext>
<viewModel:ViewModel/>
</ListView.DataContext>
</ListView>
</StackPanel>
This program is running as MVVM, so in the ViewModel I have defined New Reservation as following:
public Reservation NewReservation
{
get { return _newReservation; }
set
{
_newReservation = value;
NotifyPropertyChanged("NewReservation");
}
}
Also I have a class called Reservation with many fields - but I only want to update the hostelName, so that when a user is reserving a hostel, he will only choose from the listview of hostels and the selected item binds the name of the hostel to the NewReservation.hostelName - also Mode=TwoWay is there, so it should really work I think.
The problem is that when I also debug this program and going step by step through each line of code I see that it won't fill the NewReservation.hostelName. I've tried this also with ComboBox, but with no result, too. The interesting thing is that I have also other listview - just for viewving the hostel's details (user select a hostel from listview and then it shows the data in textblocks - this is working fine). But I really cannot realize how to send a name of the hostel from listview to NewReservation. When I've had it like a TextBox - thus user must enter the Name of the Hostel manually - it was working fine - it bind the text from textbox to NewReservation.hostelName and everything was working properly. But for some reason I cannot do this using listbox or combobox...
I was also thinking about that I will create a SelectedHostelReservation in viewmodel and bind to this guy, and then send the data from it to NewReservation.hostelName - but that's making me feeling like this is useless and making the program just longer way to do it.
So am I missing something to connect them or some logic in my binding? I really don't know.. thank you for any answer.
Here's the update requested, I'm sorry for late response : the code for the declaration of the List is this
private ObservableCollection<Hostel> _list;
public ObservableCollection<Hostel> List
{
get { return _list; }
set
{
_list = value;
NotifyPropertyChanged("List");
}
}
So it is the observable collection of Hostel class - the code for hostel class is just simple model thing with properties like name, telephone, email ...
And then we have the reservation class, which has properties like name (but now for the person who's booking the hostel), hostelName - this is the name of hostel we want to book, and many others... I have set the context for the stack panel to ViewModel - there is also this ObservableCollection defined (above) and also the NewReservation thingy... so when a say that I need to bind item source to the ObservableCollection List, and then the selected item to NewReservation.hostelName, Mode=TwoWay - it should send the data to the NewReservation should't it? Or should I make something like substep - maybe something to bind to first - like SelectedHostelName and then send it to NewReservation.hostelName?
Upvotes: 0
Views: 301
Reputation: 183
I think your List collection is the problem. Can you give me the definition of the List object. Is it the collection of the hostel names or the collection of hostels ?
One thing that is necessary in your scenario is that the List should be a collection of type string. That is the ItemSource of your ListView should have a collection of the "string" typed names of the hostels. When you chose one of those, it will return the selected name that will assign to the NewReservation.hostelName.
Upvotes: 1