Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does FallbackValue work with a MultiBinding?
    primarykey
    data
    text
    <p>I ask because it doesn't seem to work.</p> <p>Assume we're binding to the following object:</p> <pre><code>public class HurrDurr { public string Hurr {get{return null;}} public string Durr {get{return null;}} } </code></pre> <p>Well, it would appear that if we used a <strong>MultiBinding</strong> against this the fallback value would be shown, right?</p> <pre><code>&lt;TextBlock&gt; &lt;TextBlock.Text&gt; &lt;MultiBinding StringFormat="{}{0} to the {1}" FallbackValue="Not set! It works as expected!)"&gt; &lt;Binding Path="Hurr"/&gt; &lt;Binding Path="Durr"/&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; </code></pre> <p>However the result is, in fact, <strong>" to the "</strong>. Even forcing the bindings to return <code>DependencyProperty.UnsetValue</code> doesn't work:</p> <pre><code>&lt;TextBlock xmnlns:base="clr-namespace:System.Windows;assembly=WindowsBase"&gt; &lt;TextBlock.Text&gt; &lt;MultiBinding StringFormat="{}{0} to the {1}" FallbackValue="Not set! It works as expected!)"&gt; &lt;Binding Path="Hurr" FallbackValue="{x:Static base:DependencyProperty.UnsetValue}" /&gt; &lt;Binding Path="Durr" FallbackValue="{x:Static base:DependencyProperty.UnsetValue}" /&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; </code></pre> <p>Tried the same with <strong>TargetNullValue</strong>, which was also a bust all the way around.</p> <p>So it appears that <strong>MultiBinding</strong> will never ever use <strong>FallbackValue</strong>. Is this true, or am I missing something?</p> <hr> <p>A little more messing around and I found that a converter can return the UnsetValue I need:</p> <pre><code>class MultiValueFailConverter : IMultiValueConverter { public object Convert( object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (values == null || values.Length != 2 || values.Any(x=&gt;x == null)) return System.Windows.DependencyProperty.UnsetValue; return values; } public object[] ConvertBack( object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException("Too complex hurt brain."); } } </code></pre> <p>However, this seems like a dirty filthy hack. I'd think a scenario like this would be accounted for in the framework. I can't find anything in Reflector, however.</p>
    singulars
    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.
 

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