I try to bind a CheckBox in a DataGrid but in the designer I get while hovering over DoImport
cannot resolve symbol 'DoImport' due to unknown datacontext
My code is
<Window x:Class="MyWindow"> <Grid> <DataGrid x:Name="MyGrid" ItemsSource="{Binding}"> <DataGrid.Columns> <DataGridCheckBoxColumn Header="Import" Binding="{Binding Path=DoImport, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> </DataGrid.Columns> </DataGrid> </Grid> </Window> public partial class MyWindow : Window, INotifyPropertyChanged { public MyWindow(ObvervableCollection<Part> parts) { _parts = parts; MyGrid.DataContext = _parts; } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string name) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(name)); } } private ObservableCollection<Part> _parts; public ObservableCollection<Part> Parts { get { return _parts; } set { _parts = value; OnPropertyChanged("Parts"); } } } The _parts is a collection of items that fill the DataGrid. The Part class is:
public class Part { public bool DoImport { get; set; } }
CompareWindow(ObvervableCollection<Part> parts)? Shall this be the constructor for MyWindow?