How do I force an UpdateTarget on a textbox in a DataTemplate of a DataGridTemplateColumn?

May 7, 2010 at 2:33 AM
Edited May 7, 2010 at 2:35 PM

I have been stuck here for hours...

I have a wpf DataGrid with a DataGridTemplateColumn.  The DataTemplate for the column contains one text box. It has a mulitbinding converter on it, TWO-Way.  It all works fine...unless:

1) My ConvertBack method changes the value that the user types in. For example, if the user types in 50 and my ConvertBack changes it to 100, the Convert never gets called. This is because what keeps the converter from being called on the cycle back is that the PropertyChanged event is ignored if it is raised by the binding source of a two way bind. the 100 will not be displayed unless I force an "UpdateTarget"

- So if I can get to my control and call: myTextBox.GetBindingExpression(TextBox.TextProperty).UpdateTarget();

HOWEVER, this textbox is in a datatemplate that is in a DataGridTemplateColumn. The GetBindingExpression is always null -- even though the binding is on the Text property. I have read all of the examples that do this for a ListBox...But I have not seen a single example of how to do this for a datagrid.

How can I force an UpdateTarget on the textbox?  <font size="2">


</font>DataGridCell cell = WPFDataGridHelper.GetCell(pointsDataGrid, 0, 2);

TextBox tb = WPFDataGridHelper.GetVisualChild<TextBox>(cell);  // I can see in the debugger that I have the textbox that I want!<font size="2">


</font>BindingExpression be = tb.GetBindingExpression(TextBox.TextProperty);  // The binding expression is always NULL!!!!

I would much appreciate any hints! 

May 15, 2010 at 8:48 PM

I now understand this. It appears that when you have a ValueConverter and the ConvertBack changes the value that the user typed in, the Convert method gets called when the source raises the NotifyPropertyChanged event -- and the UI is therefore updated. 

However, in my case, I was using a MultiValueConverter. The ConvertBack method does not trigger a Convert call back to the target where the UI typed the entry in.  A subtle distinction that really should be better documented. Therefore if the user types in 'John' in a text box control and the MultiConverter's ConvertBack adds "Doe" to it, the UI will not be updated.  If there is a ValueConverter there, it will be updated.