Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a bit of an old question, but it could use some explanation.</p> <p>From the <a href="http://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.fallbackvalue.aspx">FallbackValue documentation</a>:</p> <blockquote> <p>A binding returns a value successfully if:</p> <ol> <li>The path to the binding source resolves successfully.</li> <li>The value converter, if any, is able to convert the resulting value.</li> <li>The resulting value is valid for the binding target (target) property.</li> </ol> <p>If 1 and 2 return DependencyProperty.UnsetValue, the target property is set to the value of the FallbackValue, if one is available. If there is no FallbackValue, the default value of the target property is used.</p> </blockquote> <p>In the example provided, the binding successfully resolves to the <code>Hurr</code> and <code>Durr</code> properties. Null is valid value for a string which means the binding is valid.</p> <p>In other words, the FallbackValue is used when the binding is unable to return a value and in the example provided, the binding does provide a valid value.</p> <p>Take for example each of the following snippets that are based off the original example:</p> <p><em>Example 1</em><br /> The Hurr and Durr properties are bound correctly; null is a valid value and the FallbackValue will never be seen.</p> <pre><code>&lt;TextBlock&gt; &lt;TextBlock.Text&gt; &lt;MultiBinding FallbackValue="Binding is valid. I will never be seen." StringFormat="{}{0} to the {1}"&gt; &lt;Binding Path="Hurr" /&gt; &lt;Binding Path="Durr" /&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; </code></pre> <p><em>Example 2</em><br /> The Hurr and Durr properties are not bound correctly; the FallbackValue will be seen.</p> <pre><code>&lt;TextBlock&gt; &lt;TextBlock.Text&gt; &lt;MultiBinding FallbackValue="Binding paths are invalid. Look at me." StringFormat="{}{0} to the {1}"&gt; &lt;Binding Path="xHurr" /&gt; &lt;Binding Path="xDurr" /&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; </code></pre> <p><em>Example 3</em><br /> If one binding path is invalid, then the FallbackValue will be seen.</p> <pre><code>&lt;TextBlock&gt; &lt;TextBlock.Text&gt; &lt;MultiBinding FallbackValue="One binding path is invalid. Look at me." StringFormat="{}{0} to the {1}"&gt; &lt;Binding Path="xHurr" /&gt; &lt;Binding Path="Durr" /&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; </code></pre> <p><em>Example 4</em><br /> As with previous examples, the binding is correct, so the FallbackValue will not be used. Further, the FallbackValue for each of the child <code>Binding</code> properties of the <code>MultiBinding</code> parent should refer to a FallbackValue to be used for the target property of the MultiBinding, not for the child Bindings.</p> <pre><code>&lt;TextBlock xmlns:base="clr-namespace:System.Windows;assembly=WindowsBase"&gt; &lt;TextBlock.Text&gt; &lt;MultiBinding FallbackValue="Binding is valid. I will never be seen." StringFormat="{}{0} to the {1}"&gt; &lt;Binding FallbackValue="{x:Static base:DependencyProperty.UnsetValue}" Path="Hurr" /&gt; &lt;Binding FallbackValue="{x:Static base:DependencyProperty.UnsetValue}" Path="Durr" /&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; </code></pre> <p><em>Example 5</em><br /> The binding is still valid even though a path is not provided in <code>Binding</code> properties since the binding will use whatever object it is bound to.</p> <pre><code>&lt;TextBlock xmlns:base="clr-namespace:System.Windows;assembly=WindowsBase"&gt; &lt;TextBlock.Text&gt; &lt;MultiBinding FallbackValue="Binding is still valid. I will never be seen." StringFormat="{}{0} to the {1}"&gt; &lt;Binding FallbackValue="{x:Static base:DependencyProperty.UnsetValue}" /&gt; &lt;Binding FallbackValue="{x:Static base:DependencyProperty.UnsetValue}" /&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; </code></pre> <p><em>Example 6</em><br /> Finally, if a converter is added to any of the Binding properties to force an UnsetValue, then the MultiBinding FallbackValue will be seen:</p> <p><strong>Converter</strong><br /></p> <pre><code>internal class ForceUnsetValueConverter : IValueConverter { #region Implementation of IValueConverter public object Convert( object value, Type targetType, object parameter, CultureInfo culture ) { return DependencyProperty.UnsetValue; } public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture ) { throw new NotImplementedException(); } #endregion } </code></pre> <p><strong>XAML</strong><br /></p> <pre><code>&lt;TextBlock&gt; &lt;TextBlock.Text&gt; &lt;MultiBinding FallbackValue="Binding is valid, but look at me. I'm an UnsetValue." StringFormat="{}{0} to the {1}"&gt; &lt;Binding Converter="{StaticResource ForceUnset}" Path="Hurr" /&gt; &lt;Binding Path="Durr" /&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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