Search code examples
c#wpfdata-bindingwpfdatagrid

WPF Binding: cannot resolve symbol due to unknown datacontext


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; }
}

Solution

  • The designer cannot supply a ObvervableCollection<Part> instance to the MyWindow constructor, because it does not know how. so your constructor should never be called in the designer. that is why the designer "cannot resolve symbol 'DoImport' due to unknown datacontext". the designer in visual studio expects a parameter-less default constructor in order to work properly.

    maybe you should check that out: http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/30/sample-data-in-the-wpf-and-silverlight-designer.aspx or better yet ... let blend create design sample data for you.