当绑定返回 null 时隐藏多重绑定字符串格式

发布于 2024-10-09 14:47:03 字数 582 浏览 0 评论 0原文

我正在寻找将时间跨度属性绑定到文本块,这似乎可以通过以下方式解决 这篇帖子的帮助

现在我想在数据为空时隐藏 StringFormat。如果我使用带有 a 的多绑定,则从线程中stringformat,如果我的数据为空,那么 stringformat 仅显示一个“:”

<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>

如果数据为空,我如何隐藏“:”

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸢与 2024-10-16 14:47:03

我基本上在这里回答了与 Nicolas Repiquet 相同的问题,但无论如何..
感觉这里的框架缺少了一部分。 (据我所知)没有办法让 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();
    }
}

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();
    }
}
梦毁影碎の 2024-10-16 14:47:03

此处查看多重绑定后备值。

Take a look at multibinding fallback value here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文