views:

70

answers:

3

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.

A: 

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.

Steve Danner
Miles on from that mate...got the two way I pass in a Collection<bool> bound to 7 togglebuttons (these buttonsshould have no logic, i.e checked or unchecked)...how can the underlying collection<bool> be updated?I think a wrapper around a Collection<bool> with collectionchanged routed events should work but I cant make this work without hacks.Thanks U.
A: 

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.

Jeff Wain
I know, and I have done so....again this doesnt help with no code behind updates.......for example if you have an object with an ObservalbeCollection<bool> bound to togglebuttons, most people would have a Checked and Unchecked event associated and handle this there....I dont want these events, nor should you need them (so im told). If i have a object called ArrayBool which accepts a enum of DayOfWeek, and transposes this DayOfWeek into a ArrayBool....(continued below)
i then have an object which has a dependencyproperty of ArrayBool, and here im binding my togglebuttons to the bool values in my ArrayBool.....now I dont want any update logic in the events for checked and unchecked, instead I want it so that my collection (ArrayBool) delegates and watches the collection (as well as the togglebuttons) and notifies everyone else on the change.Thanks for the input. U.
Could you include the code you're using in your source collection and bindings? Might just be that you're missing a binding property like NotifyOnSourceUpdated or BindsDirectlyToSource.
Jeff Wain
Done all this as well, ive even rewrote all event handlers for everything and made custom togglebuttons etc etc but it just ends up in a brutal hack to do what I want (or what Ive been told that can be done)....ive spent three whole days looking at this and I have got nowhere.....Im afraid I cant show the code as I am bound for commercial reasons, i just dont no how to fix this problem...though if you want to take this offline so I could provide you with a simplistic version of the code, Id be happy to do so. And then post the solution up on stackoverflow itself. Thanks U.
You can email me at jeff (at) jeffwain . com
Jeff Wain
Excellent, jeff thank you....Ill email you soon with a partial but very similar solution.Thanks U.
Sorry Jeff this refactoring is turning out to be a pain.....it will take me another day or so...thanks
Still working on this...in between everything else......anybody any ideas?
+1  A: 

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!

Alex Humphrey
Thanks very much, and this will be very useful for those with bool updating problems.....however my problem is not the bools themselves updating thats fine, its the collection that is being changed in the view these changes are not routing down to the original collection to which the binding is set.e.g.
Binding b = new Binding(paramName) { Source = WorkItem };b.Mode = BindingMode.TwoWay;Now i set this binding to the dependency property of my custom usercontrol which contains the 7 togglebuttons.DaySelector.SetBinding(DaySelector.WeekProperty, b);Now when the WeekProperty dependencyproperty is changed in the usercontrol and the view, the WorkItem (the source of the binding) its paramName is not being notified of such changes.......i was also told that the checked and unchecked events on the togglebuttons are not needed.Thanks U.