0
votes

I have a UserControl that I want to use to edit data in a DataGrid. The UserControl has a TextProperty that I use for binding. When editing a cell, the content of the TextProperty is displayed. But when leaving the edit mode, the TextPropery of my item is not updated.

Here I found a solution: C# Wpf Editing Datagrid does not update it's itemsource

For a TextBox this binding is working.

factoryTextBox.SetBinding(TextBox.TextProperty, new Binding("Title"));

But for my CustomTextBox it only works with setting mode to TwoWay mode.

factoryTextBox.SetBinding(CustomTextBox.TextProperty, new Binding("Title") {
  Mode = BindingMode.TwoWay // <--
});

Why do I need TwoWay for my user control, but not for TextBox. Do I miss something?

1

1 Answers

1
votes

The textproperty of the textbox has the metadata set for it making it bound twoway by default. You want something very roughly like:

  public static readonly DependencyProperty TextProperty =
   DependencyProperty.Register(
     "TextProperty",
     typeof(string),
     typeof(CustomTextBox),
     new FrameworkPropertyMetadata(string.empty
 , FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));