Ive got a WPF-DataGrid to show nHibernate data. The xaml looks like this:
<DataGrid x:Name="myDataGrid" AddingNewItem="myDataGrid_AddingNewItem" CanUserAddRows="true"/>
To show the Data in the dataGrid I use this code
public MainWindow()
{
InitializeComponent();
var erg = from p in m_Dbsession.Query<LovTestData>()
select new LovTestDataDatasource
{
SomeString = p.SomeString,
SomeOtherString = p.SomeOtherString
};
myDataGrid.ItemsSource = erg.ToList();
}
My DatasourceClass looks like this:
class LovTestDataDatasource : IEditableObject
{
public LovTestDataDatasource()
{ }
public string SomeOtherString { get; internal set; }
public string SomeString { get; internal set; }
public void BeginEdit()
{
}
public void CancelEdit()
{
}
public void EndEdit()
{
}
}
The problem is, that I cant see a possibility to add new rows. There is a empty row at the bottom but its not editable. the insert-key also does not work and myDataGrid_AddingNewItem is never called.
What Im doing wrong?
Edit:
It seems to have something to do with my Datasource-class because when I change the ItemsSoure setting to:
var erg = from p in m_Dbsession.Query<LovTestData>()
select p;
myDataGrid.ItemsSource = erg.ToList();
I can edit the row and myDataGrid_AddingNewItem is called.
Another Edit:
I tried:
var erg = from p in m_Dbsession.Query<LovTestData>()
select new LovTestDataDatasource
{
SomeString = p.SomeString,
SomeOtherString = p.SomeOtherString
};
myDataGrid.ItemsSource = new ObservableCollection<LovTestDataDatasource>(erg.ToList());
but this didnt work.
Change the internal setters in your Datasource class to public.
public string SomeOtherString { get; set; }
public string SomeString { get; set; }
If you leave them as internal, the PresentationFramework has no accessible setters for these properties (because they are from different assembly). They are considered readonly and thus can't be edited from the DataGrid.