If i have a Collection bound to n togglebuttons in a stackpanel in a usercontrol….how can I update the underlying Collection with no code behind (including Checked and unchecked events) and complete update logic?
Thanks,
U.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Your question is vague, but I appreciate why that is (commercial stuff). We can therefore only guess at what the problem may be.
I’ll try my best…
It sounds like you want seven ToggleButtons, each button activating/deactivating a day of the week. You have a collection of 7 Boolean values. Each ToggleButton’s IsChecked property is bound to a Boolean in the collection.
The problem is that at the moment, you are trying to change the object in the collection, rather than just a property of that object. I don’t know if straight swapping of items at certain positions is supported my ObservableCollection or not, but it’s certainly possible that the WPF binding framework does not support straight swaps of objects in collections. You can add and remove items, but not do straight swaps at certain positions.
You can however work around this (possible) limitation.
Try creating a new class which implements INotifyPropertyChanged like so:
(If you’re not familiar with INotifyPropertyChanged it’s pretty simple – there are plenty examples on MSDN. It allows the binding framework to detect property changes)
Instead of having an ObservableCollection<Boolean>, have an ObservableCollection<BooleanWrapper>. Each ToggleButton’s IsChecked property should be bound to the BooleanWrapper’s IsSelected property.
Now you’re not trying to swap objects in and out of the collection, you’re just updating the value of a property of an object within the collection.
As a side note, if you’re only binding to days of the week, in my opinion there’s nothing wrong with binding to a class like:
which should give you no problems, but that’s up to you, and I may have made errors in my psychic requirements capture – please ignore if it’s not relevant to what you want.
I hope I’ve made some sense!