There are a few things you need to change here to make this work. First, you will need to wrap your boolean in an object that implements the INotifyPropertyChanged interface to receive a notification of the change you are looking for. You are currently bound to booleans in your collection that do not implement the interface. To do this, you can create a wrapper class as follows:
public class Wrapper: INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } private bool val = false; public bool Val { get { return val; } set { val = value; this.OnPropertyChanged("Val"); } } public Wrapper(bool val) { this.val = val; } }
Then you want to create these objects in your form instead of a list of gates. You can also use an observable collection instead of a list to send notification of added and deleted items. This is shown below:
public Window1() { InitializeComponent(); this.DataContext = this; } private ObservableCollection<Wrapper> myCollection = new ObservableCollection<Wrapper>() {new Wrapper(true), new Wrapper(false), new Wrapper(true)}; public ObservableCollection<Wrapper> MyCollection { get { return myCollection; } }
The next thing to do is show a list of checkboxes in your ui. For this, WPF provides itemscontrols functions. ListBox is a control for items, so we can use this as a starting point. Set itemssource in the list as MyCollection. Then we need to determine how each Wrapper object will appear in the list, and this can be done using the datatemplate that is created in the Windows resources. This is shown below:
<Window.Resources> <DataTemplate x:Key="myCollectionItems"> <CheckBox IsChecked="{Binding Path=Val, Mode=TwoWay}"></CheckBox> </DataTemplate> </Window.Resources> <Grid> <ListBox ItemsSource="{Binding Path=MyCollection}" ItemTemplate="{StaticResource myCollectionItems}"></ListBox> </Grid>
This should make you work with a simple demonstration of checkboxes that have values ββtied to a list of gates.
source share