Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy would putting a no-op Converter on a Binding change its behavior?
    text
    copied!<p>I'm in the midst of testing a user control I've built, and I'm encountering something that's inexplicable to me. </p> <p>The control's an extension of the ComboBox that handles values of a specific custom type. It has a dependency property of that custom type that is the target property of a Binding.</p> <p>I've got a trace statement in the setter, and I can see that the property is getting set. But it's not appearing in my user control.</p> <p>Now, ordinarily I'd say, okay, I've got a bug in my user control. I probably do, though I'm baffled about it. But this question isn't about finding the bug in my control. Read on; here is where it gets weird.</p> <p>I'm also using Bea Stollnitz's little value converter to help debug the Binding:</p> <pre><code>public class DebuggingConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return value; // Add the breakpoint here!! } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException("This method should never be called"); } } </code></pre> <p>The idea behind this is that I add this converter to my Binding and can set a breakpoint to see what value is being pushed out to the target. Okay, that works just fine. I can see that the value is being pushed out.</p> <p>In fact, it works a little <em>too</em> fine. If the DebuggingConverter is attached to the Binding, the user control displays the value. If it's not, it doesn't.</p> <p>How is that even possible? How could a value converter that does nothing affect the behavior of a bound control?</p> <p><strong>Edit:</strong></p> <p>Not that it's likely to help, but here's the XAML for the user control:</p> <pre><code>&lt;a:CodeLookupBox Grid.Column="1" Grid.IsSharedSizeScope="True" MinWidth="100" Style="{Binding Style}"&gt; &lt;a:CodeLookupBox.CodeLookupTable&gt; &lt;Binding Path="Codes" Mode="OneWay"/&gt; &lt;/a:CodeLookupBox.CodeLookupTable&gt; &lt;a:CodeLookupBox.SelectedCode&gt; &lt;Binding Path="Value" Mode="TwoWay" ValidatesOnDataErrors="True"/&gt; &lt;/a:CodeLookupBox.SelectedCode&gt; &lt;/a:CodeLookupBox&gt; </code></pre> <p>Without the converter on the second binding, the control behaves as though I didn't set <code>SelectedCode</code>. Even though a trace statement in the <code>OnSelectedCodePropertyChanged</code> handler shows that <code>e.Value</code> does indeed contain the correct value. This happens irrespective of whether the converter's attached or not.</p> <p>I've been trying to reverse-engineer this problem with a thought experiment: if you wanted to create a bound user control whose behavior changed if a no-op converter were attached to its binding, how would you do it? I don't know enough about binding to come up with an answer.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload