Reputation: 731
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.
Upvotes: 0
Views: 274
Reputation: 1045
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});
}
}
}
Upvotes: 1