views:

75

answers:

1

Hi,

I have a collection of bools associated to days of the week - Collection() { true, false, false, false, false, false,false}; So whatever the bool represents means that this collection applies for sunday (sunday being the first day of week here).

Now I have set the itemssource of my listbox, say, to be this collection.

<ListBox ItemsSource={Binding Path=Collection, Mode=TwoWay}>
     <ListBox.ItemTemplate>
         <ToggleButton IsChecked={Binding Path=BoolValue, Mode=TwoWay}/>
     </ListBox.ItemTemplate>
</ListBox>

However my Collection never gets updated (my collection is a dependencyproperty on the window). Plus "MyBool" class is simply just a wrapper around a bool object, with NotifyPropertyChanged implemented.

Any ideas.....my actual code is majorly complex so the above situation is a brutally simplified version, so make assumptions etc if necessary Ill work around this given that I have provided my actual code.

Thanks greatly in advance,

U.

A: 

Try to set UpdateSourceTrigger=PropertyChanged in your binding

<ToggleButton IsChecked={Binding Path=BoolValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}/>

edit: created a small example, seems fine.

The wrapper class

    public class MyBool : INotifyPropertyChanged
{
    private bool _value;

    public bool Value
    {
        get { return _value; }
        set
        {
            _value = value;
            NotifyPropertyChanged("Value");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

The XAML

       <ListBox ItemsSource="{Binding Path=Users}" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <ToggleButton IsChecked="{Binding Path=Value, Mode=TwoWay}" Content="MyButton"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Code behind

 /// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public ObservableCollection<MyBool> Users { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        Users = new ObservableCollection<MyBool>();
        DataContext = this;
        Loaded += new RoutedEventHandler(MainWindow_Loaded);

    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        FillUsers();
    }

    private void FillUsers()
    {

        for (int i = 0; i < 20; i++)
        {
            if(i%2 == 0)
                Users.Add(new MyBool { Value = true });
            else
                Users.Add(new MyBool {  Value = false});
        }
    }
}
hkon
That also seems to do nothing im afraid....going to take another look in the morning so if discover anything Ill reply
I cant seem to do this without some sort of code behind to help with the updating....does anyone have any idea with direct binding???Thanks U
is the xaml posted your exact code?
hkon
Its very similar....I cant give you the exact code due to copyright problems etc. I just really want to know how to approach such a binding. I have an object with a colection of bools, bound to a set of 7 togglebutton, how is the updating logic performed?
FYI the one way binding etc works fine...
Any ideas? Anyone? Im stuck here...can seem to perform the the two way binding without some hack.....
Collection<bool> update logic????
The update logic is gained by the type in the collection implementing INotifyPropertyChanged, bool does not do this and thus must be wrapped in a class which does.
hkon