WPF 绑定 FallbackValue 设置为 Binding

发布于 2024-08-15 01:01:31 字数 247 浏览 9 评论 0原文

有没有办法让另一个绑定作为后备值?

我正在尝试做这样的事情:

<Label Content="{Binding SelectedItem.Name, ElementName=groupTreeView,
                         FallbackValue={Binding RootGroup.Name}}" />

如果有人有另一个技巧来实现它,那就太好了。

Is there a way to have another binding as a fallback value?

I'm trying to do something like this:

<Label Content="{Binding SelectedItem.Name, ElementName=groupTreeView,
                         FallbackValue={Binding RootGroup.Name}}" />

If anyone's got another trick to pull it off, that would be great.

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

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

发布评论

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

评论(3

稍尽春風 2024-08-22 01:01:31

您正在寻找的是称为 PriorityBinding 的东西( 列表)

(来自文章)

PriorityBinding 的要点是
按顺序命名多个数据绑定
从最理想到最不理想。
这样如果第一次绑定失败,
为空和/或默认值,另一个
绑定可以取代它。

例如

<TextBox>
    <TextBox.Text>
        <PriorityBinding>
            <Binding Path="LastNameNonExistant" IsAsync="True" />
            <Binding Path="FirstName" IsAsync="True" />
        </PriorityBinding>
    </TextBox.Text>
</TextBox>

What you are looking for is something called PriorityBinding (#6 on this list)

(from the article)

The point to PriorityBinding is to
name multiple data bindings in order
of most desirable to least desirable.
This way if the first binding fails,
is empty and/or default, another
binding can take it's place.

e.g.

<TextBox>
    <TextBox.Text>
        <PriorityBinding>
            <Binding Path="LastNameNonExistant" IsAsync="True" />
            <Binding Path="FirstName" IsAsync="True" />
        </PriorityBinding>
    </TextBox.Text>
</TextBox>
诗化ㄋ丶相逢 2024-08-22 01:01:31

如果您在绑定到 null 值和 PriorityBinding 时遇到问题(正如 Shimmy 指出的那样),您可以使用 MultiBinding 和 MultiValueConverter,如下所示

public class PriorityMultiValueConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return values.FirstOrDefault(o => o != null);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

<TextBox>
    <TextBox.Text>
        <MultiBinding Converter="{StaticResource PriorityMultiValueConverter}">
            <Binding Path="LastNameNull" />
            <Binding Path="FirstName" />
        </MultiBinding>
    </TextBox.Text>
</TextBox>

If you run into problems with binding to null values and PriorityBinding (as Shimmy pointed out) you could go with MultiBinding and a MultiValueConverter like that:

public class PriorityMultiValueConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return values.FirstOrDefault(o => o != null);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Usage:

<TextBox>
    <TextBox.Text>
        <MultiBinding Converter="{StaticResource PriorityMultiValueConverter}">
            <Binding Path="LastNameNull" />
            <Binding Path="FirstName" />
        </MultiBinding>
    </TextBox.Text>
</TextBox>
吻泪 2024-08-22 01:01:31

您希望在什么条件下使用 Fallback 值?您如何确定绑定失败?即使绑定到空值,绑定仍然有效。

我认为一个好的选择可能是使用转换器在绑定返回 null 时转换为默认值。我不确定你如何默认为另一个绑定值。

此处查看转换器

Under what conditions would you like it to use the Fallback value? How would you determine that a binding has failed? A binding is still valid even if it's bound to a null value.

I think a good bet may be to use a converter to convert to a default value if the binding returns null. I'm not sure how you could default to another bound value though.

Check out converters here

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