WPF Two Way Data Binding

I never saw this before – I wanted to make a configuration parameters Datagrid on a cpu load gauge.

I could get the Datagrid values to edit and change, but the changes did NOT propagate back to the actual object data.

WPF_2_way_bindingI found a teeny-weeny line buried down in a StackOverflow post from a year ago that mentioned an additional parameter on the Binding tag:  UpdateSourceTrigger

I had to tell it when to push the changes back to the object.  I thought just saying Mode=TwoWay (for classes that derive from INotifyPropertyChanged) was enough.

XAML Portion
<DataGridTemplateColumn Header=”Param Value” Width=”100″ IsReadOnly=”False”>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text=”{Binding ParamValue,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}” />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

The choices for UpdateSourceTrigger are

  • LostFocus
  • Explicit
  • PropertyChanged
  • Default (this is usually ‘LostFocus’ )

I made mine PropertyChanged because the values are short and the user might not tab-out  before clicking ‘Apply’.

But each single-letter change in the TextBox is echoed back at the ConfigItems object.

 

Proactive technologist.

Posted in dot Net, New, WPF

What do you think?