Note that there are some explanatory texts on larger screens.

plurals
  1. PODatabinding phenomenon in WPF (binding to a FrameworkElement) - any ideas why?
    text
    copied!<p>In an application of mine (this has to do with very dynamic navigation and content presentation) I have to use this construct in XAML:</p> <pre><code>&lt;ContentControl Content={Binding ContentElement} /&gt; </code></pre> <p>So far, so good. This is great, absolutely great. I can host arbitrary stuff all over the place.</p> <p>But there seems to be a strange, well, lets call it "phenomenon" in WPF (I believe it is in the BindingMarkupExtension, but not sure yet):</p> <p>When my ContentElement property looks like this:</p> <pre><code>public FrameworkElement ContentElement { get { return this.m_ContentElement; } } </code></pre> <p>then the getter gets called TWICE (!!!) for each databinding operation (this includes when the user changes the language on the fly or reloads the hosting control).</p> <p>However (and this is what is REALLY mind boggling for me):</p> <p>When I change my ContentElement property to:</p> <pre><code>public object ContentElement { get { return this.m_ContentElement; } } </code></pre> <p>then the getter gets called once. Seriously, I'm not lying here. It is absolutely reproducible in the simplest applications, you can try for example by returning a new "TextBlock" (thats what I usually do to test or learn about more advanced WPF concepts).</p> <p>Any ideas why?</p> <p>The reason why I ask is that I hate the following consequences of the solution:</p> <ul> <li>I lose type safety at this point</li> <li>This may be a bit hard to explain to new developers or overly sceptical wisecracks</li> </ul>
 

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