Binding UserControl to a NULL DataContext

Posted by user634981 on Stack Overflow See other posts from Stack Overflow or by user634981
Published on 2011-02-25T23:09:02Z Indexed on 2011/02/25 23:25 UTC
Read the original article Hit count: 310

Filed under:
|
|
|
|

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!

© Stack Overflow or respective owner

Related posts about wpf

Related posts about usercontrols