Hi,
I have created a datagrid with DataGridTextcolumn dynamically. Few columns are read only and few are not. Based on the user action I need to set the read only for a specific cell in data grid. I need to do everything dynamically. For this I have created DataGridTextColumn with read only as false. After that based on user actions and conditions, setting the cell as either editable or not. For that I did like below.
//////////////////////////////////////// Code /////////////////////
Style style = new Style(typeof(DataGridCell));
DataTrigger dt = new DataTrigger();
dt.Binding = new Binding(a_editableBindingPropertyPath);
ControlTemplate clt = new ControlTemplate();
clt.TargetType = typeof(DataGridCell);
Binding bd2 = new System.Windows.Data.Binding();
bd2.RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent);
bd2.Path = new PropertyPath("Content.Text");
bd2.Mode = BindingMode.TwoWay;
bd2.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
FrameworkElementFactory m_borderFWElementFactory = new FrameworkElementFactory(typeof(Border));
FrameworkElementFactory m_textFWElementFactory = new FrameworkElementFactory(typeof(TextBox));
m_textFWElementFactory.SetBinding(TextBox.TextProperty, bd2);
m_textFWElementFactory.SetValue(TextBox.IsReadOnlyProperty, false);
m_textFWElementFactory.SetValue(TextBox.BackgroundProperty, Brushes.White);
m_borderFWElementFactory.AppendChild(m_textFWElementFactory);
clt.VisualTree = m_borderFWElementFactory;
Setter s = new Setter(DataGridCell.TemplateProperty, clt);
dt.Value = "true";
dt.Setters.Add(s);
style.Triggers.Add(dt);
dataGridTextColumn.CellStyle = style;
////////////////////////////////////////
But I am facing one problem. Whenever text in cell changes, its not updating the binding source.
Can somebody suggest me what could be done.
Regards,
Mohan.