I am trying to set the binding for a Data Grid Column Headers text during the Auto generating Column event but with no luck. headerDetails is a dictionary containing columnSettings objects that implement the INotifyPropertyChanged interface (Header setter raises an OnPropertyChanged event)
private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
e.Column.Header = this.headerDetails[headername].Header;
//// rather than set the value here, create a binding
}
I have tried looking at these examples mentioned and came up with this:
Binding myBinding = new Binding();
myBinding.Source = this.headerDetails[headername].Header;
myBinding.Path = new PropertyPath("Header");
myBinding.Mode = BindingMode.TwoWay;
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(e.Column, TextBox.TextProperty, myBinding);
which unfortunately doesn't work :(
MM8 answer has fixed the problem thanks, I was Binding to the variable rather than the object. the solution with notes:
Binding myBinding = new Binding();
myBinding.Source = this.headerDetails[headername]; // Source = object
myBinding.Path = new PropertyPath("Header"); // Path = Getter/Setter
myBinding.Mode = BindingMode.TwoWay;
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(e.Column, DataGridTextColumn.HeaderProperty, myBinding);