I have this code to bind datagrid in the front end to the elementDetail property in my view model. elementDetail is defined as an instance of my own INotifyPropertyChanged class. But why the PropertyChanged handle is always null? Is it the right way to bind a datatable to a datagrid and datagrid should respond to the changes to datatable?
What is the best way to debug WPF programs? For binding, I turned on diag:PresentationTraceSources.TraceLevel=High, but the information it provides is not very much.
XAML
<Grid Name="myGrid" Background="#FFF3FF76">
<DataGrid Name="detailGrid"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="10,57,0,0"
Width="Auto"
Height="56"
Background="#FFFFDF9F"
ItemsSource="{Binding elementDetail, Mode=OneWay,UpdateSourceTrigger=PropertyChanged, diag:PresentationTraceSources.TraceLevel=High}" />
</Grid>
code behind .xaml
this.myGrid.DataContext = myViewModelInstance;
in my ViewModel
public ObservableDataView elementDetail
{
get;
set;
}
modify the datatable
elementDetail.data = dt;
I defined my INotifyPropertyChanged class as here
public class ObservableDataView : INotifyPropertyChanged
{
private string _propertyName;
public ObservableDataView(string propertyName)
{
_propertyName = propertyName;
}
private DataTable _data;
public DataTable data
{
get{ return _data; }
set
{
_data = value;
onChanged(_propertyName);
}
}
public void onChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
PropertyChanged(this, e);
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
Edit: Answer to my question
I should say onChange("data")
I should also do is to add .data in my binding as below
<DataGrid Name="detailGrid" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,57,0,0" Width="Auto" Height="56" Background="#FFFFDF9F" ItemsSource="{Binding elementDetail.data, Mode=OneWay,UpdateSourceTrigger=PropertyChanged, diag:PresentationTraceSources.TraceLevel=High}" />
It is now working fine. Is there a way to debug bindings or is there any general rule to bind? I found the major problem I am facing with WPF currently is wrong binding.