3

I was looking to bind a timespan property to a textblock,which seems to be resolved with the help of this post

Now i want to hide StringFormat when data is null.From the thread if i use a mutibinding with a stringformat and if my data is null then the stringformat displays just a ":"

<TextBlock>
    <TextBlock.Text>
        <MultiBinding StringFormat="{}{0}:{1}">
            <Binding Path="MyTime.Hours" TargetNullValue={x:Null}/>
            <Binding Path="MyTime.Minutes" TargetNullValue={x:Null}/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

How could i hide the ":" if data is null

Community
  • 1
  • 1
biju
  • 17,554
  • 10
  • 59
  • 95

2 Answers2

5

I'm basically answering the same thing as Nicolas Repiquet here but anyway..
It feels like there is a part missing in the framework here. There is no way (as far as I know) to make a MultiBinding use a FallbackValue without a Converter. Using this approach will probably set you back to square 1 since your last question was about a better approach then using a Converter :)

<TextBlock>
    <TextBlock.Text>
        <MultiBinding StringFormat="{}{0}:{1}"
                      Converter="{StaticResource FallbackConverter}"
                      FallbackValue="">
            <Binding Path="MyTime.Hours" />
            <Binding Path="MyTime.Minutes" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

And the Converter basically does what you "should" be able to use a Property for

public class FallbackConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        foreach (object value in values)
        {
            if (value == DependencyProperty.UnsetValue)
            {
                return DependencyProperty.UnsetValue;
            }
        }
        return values;
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
2

Take a look at multibinding fallback value here.

Community
  • 1
  • 1
Nicolas Repiquet
  • 9,097
  • 2
  • 31
  • 53