开发者

Hide multibinding stringformat when binding returns null

开发者 https://www.devze.com 2023-02-02 07:36 出处:网络
I was looking to bind a timespan property to a textblock,which seems to be resolved with the help of this post

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


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


Take a look at multibinding fallback value here.

0

精彩评论

暂无评论...
验证码 换一张
取 消