WPF 绑定 FallbackValue 设置为 Binding
有没有办法让另一个绑定作为后备值?
我正在尝试做这样的事情:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在寻找的是称为 PriorityBinding 的东西(此 列表)
(来自文章)
例如
What you are looking for is something called PriorityBinding (#6 on this list)
(from the article)
e.g.
如果您在绑定到 null 值和 PriorityBinding 时遇到问题(正如 Shimmy 指出的那样),您可以使用 MultiBinding 和 MultiValueConverter,如下所示
:
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:
Usage:
您希望在什么条件下使用 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