将 UserControl 绑定到 NULL DataContext
我有一个 UserControl 并将其 DataContext 绑定到一个对象。我还将 UserControl 的 IsEnabled 属性绑定到该对象的布尔属性,例如:
<my:MyUserControl DataContext="{Binding Items.SelectedItem}" IsEnabled="{Binding Path=IsEditable}"/>
如果 Items.SelectedItem 不为空,则效果很好。但是,如果它为 null(如果 Items 集合为空,有时会发生这种情况),则不会评估 IsEnabled 绑定并设置为 true,这不是所需的行为。
我尝试过使用 MultiBinding 但没有成功,因为我不知道是否可以绑定到 DataContext。我也尝试过使用 DataTrigger,但再次没有成功。
有人请指出我应该采取的正确方法的正确方向吗?
谢谢!
I have a UserControl and am binding its DataContext to an object. I also bind the IsEnabled property of the UserControl to a boolean property of that object eg:
<my:MyUserControl DataContext="{Binding Items.SelectedItem}" IsEnabled="{Binding Path=IsEditable}"/>
This works fine provided Items.SelectedItem is not null. However, if it is null (which can happen sometimes if the Items collection is empty), the IsEnabled binding does not get evaluated and is set to true, which is not the desired behaviour.
I've tried using a MultiBinding but without success because I don't know if it's possible to bind to the DataContext. I've also tried using a DataTrigger, but again without success.
Would somebody kindly point me in the right direction as to the correct way I should be doing this.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试:
Try:
您是否尝试过 FallbackValue=False ?
Have you tried FallbackValue=False ?
请参阅
Binding.FallbackValue
。See
Binding.FallbackValue
.