Hide multi-binding stringformat when binding returns null

I was looking for a binding of the timespan property to a text block, which seems to help this post

Now I want to hide StringFormat when the data is null. From a stream, if I use mutibinding with a string format, and if my data is zero, then stringformat only displays ":"

<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 can I hide the ":" if the data is null

+3
source share
2 answers

, , ..
, . ( ) MultiBinding FallbackValue . , , 1, , :)

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

, ""

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();
    }
}
+5

.

+2

Source: https://habr.com/ru/post/1782800/