❔ ❔ Bind selected items
In my MVVM, I have connected view and viewModel to display "Months" list, but not sure how to bind and send through selected ones. Could anyone help me with binding?
15 Replies
Did you set the ViewModel to the DataContext of your View?
like this:
Yes -
ok, the ItemsTemplate is unnecessary
the listbox can manage text alone
They are populated, but I'd like to send selected to the model
You can make a additional property like:
public string SelectedItem {get; set:}
and bind the Listbox propertye SelectedItem to it.
Then the property in the ViewModel will have the value of the selection from the Listbox
another hint, initialize your list in the constuctor. Then return the private field _month in the getter of the property.
Instead of creating a new list each time.Thank you @dancepanda42, I've added property and it grabs string properly
Is it possible to send it as list, considering I have a multiple selection?
yes, but this requires a little more code.
To clarify.
The SelectableItem gets the information that it is selected from the ListBoxItem through the binding in the style.
To display the values, another property is now needed in the SelectableItem. This can also be displayed by an ItemTemplate via a binding.
This example should work
I'll give it a go
ObservableObject is part of some external package?
yes its form the CommunityToolkit.Mvvm
makes it more comfortable to write the mvvm pattern.
but you can also use your ViewModelBase
Binding now works, thank you!
your welcome
I'll try to play around for a bit, bind to models, would it be okay if I'll keep question open for some time and ask you if I'm stuck?
sure am but in the Berlin time zone... so I'm no longer so long online.
but you can send me a PM then I answer as soon as I find time.
Thank you @dancepanda42 🙂
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.
Was this issue resolved? If so, run /close
- otherwise I will mark this as stale and this post will be archived until there is new activity.