Let me prefix this question by stating that I\'m very new to both C# and WPF.
I\'m trying to connect a collection of Boolean values to a container conta
Use IsChecked="{Binding}" to bind the item of the collection directly.
However it is not possible to do bind to source with this method. Because the binding on the IsChecked property of the CheckBox doesn't the index of the item of the binding. So, it can't change the collection but only the item of the collection.
To get around that limitation, you can create a wrapper to the Boolean value:
public class Wrapper : INotifyPropertyChanged
{
private T value;
public T Value
{
get { return value; }
set
{
{
this.value = value;
OnPropertyChanged();
}
}
}
public static implicit operator Wrapper(T value)
{
return new Wrapper { value = value };
}
public static implicit operator T(Wrapper wrapper)
{
return wrapper.value;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
Here is an exemple of usage:
public partial class MainWindow : Window
{
public ObservableCollection> MyBooleanCollection { get; private set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
MyBooleanCollection = new ObservableCollection>() { false, true, true, false, true };
}
}
And in the XAML: