0

This Winforms app works fine, but I'm now trying to add a bound WPF UserControl to it via ElementHost, and my attempts at binding it have no effect.

Left two thirds of screen capture are normal databound Winforms controls. Right third of screen capture is my WPF UserControl (which houses a ToolBar, RichTextBox, and a (strictly for debugging, will be removed later) plain TextBox.)

Expected Result: When I switch projects in leftmost pane, (WPF) "HelloText" should be replaced with the text seen in the Problem Statement" (Winforms) TextBox.

Actual Result: "HelloText" never changes.

  1. Yes, my domain object implements INotifyPropertyChanged.
  2. Yes, I implemented "Text" and "Document" DependencyPropertyies in the root of the WPF UserControl that do modify the inner controls' values (works when written to in my throwaway WPF tester app, but not the Winforms app seen in the screen capture.)
  3. I adapted my Winforms-to-WPF binding code from the accepted solution in this question: https://stackoverflow.com/a/12054082/2112855
  4. Breakpoints in the WPF UC's setters are never hit when running the Winforms app.

screen capture

            System.Windows.Data.Binding wpfb2 = new System.Windows.Data.Binding("ProblemStatement") // WPF
            {
                Source = _bs,
                UpdateSourceTrigger = System.Windows.Data.UpdateSourceTrigger.PropertyChanged
            };   
            rteProblemStatement.SetBinding(WpfRichTextEditor.RichTextEditor.TextProperty, wpfb2);
amonroejj
  • 573
  • 4
  • 16
  • IMO, it's better to add the DataBinding to the ElementHost and use its `PropertyMap` to add a `PropertyTranslator` delegate, to set the Text property of the WPF Control using the value passed by the delegate. To add a a Binding to the ElementHost, use the standard `[ElementHost].DataBindings.Add("Text", [The BindingSource instance], "ProblemStatement", true, DataSourceUpdateMode.OnPropertyChanged);` then add the delegate: `[ElementHost].PropertyMap.Add("Text", new PropertyTranslator(OnTextChanged));`, where `OnTextChanged` is the method delegate that receives the string value to set. – Jimi Mar 08 '21 at 23:21
  • Otherwise, you can probably add a ViewModel to the UC and use its public properties as a *map* for the Properties you want to bind (the DataContext is set to the ViewModel object). – Jimi Mar 08 '21 at 23:27
  • This being my first exposure to ElementHost, I wasn't even aware of the existence of PropertyTranslator :/ But yeah, that worked :) Upgrade your comment to an answer and I'll accept it. – amonroejj Mar 09 '21 at 14:00
  • Can PropertyTranslator round-trip this (write edited text back to the domain object)? – amonroejj Mar 09 '21 at 14:03
  • (Google is of no help here. Searching for [winforms "propertymap" "two way"] gives about a dozen garbage hits.) – amonroejj Mar 09 '21 at 14:25

0 Answers0