Hi,
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.
Hi,
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.
Have a look at TwoWay binding mode. That will allow underlying business objects to be updated by the UI as well as having the UI updated by the underlying business objects.
In order to leverage a collection for binding you'll need to check out ObservableCollection<T>
(MSDN link). By using that, you should subscribe to most of the binding you're looking for automatically. If you happen to be using something other than a bool
for your objects, though, you'll have to implement INotifyPropertyChanged
on whatever object you decide to bind to.
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:
class BooleanWrapper : INotifyPropertyChanged
{
private Boolean isSelected;
public Boolean IsSelected
{
get { return isSelected; }
set
{
if (isSelected != value)
{
isSelected = value;
// TODO: Raise PropertyChanged event.
}
}
}
}
(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:
class DaySelection : INotifyPropertyChanged
{
public Boolean IsMondaySelected { ... }
public Boolean IsTuesdaySelected { ... }
...
}
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!