I have a value converter that converts null to Visibility.Collapsed. Now I try to use it in a user control so that the whole control would collapse when it's DateContext is null
The control looks like this:
<UserControl x:Class="PhoneApp.Controls.Header"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Visibility="{Binding Converter={StaticResource ToVisibility}}"
d:DesignHeight="150" d:DesignWidth="480"> <-- some body here -->
And it is used like this
<my:Header DataContext="{Binding SectionHeader}"/>
Where SectionHeader is a view model property.
I hava a similar control where I don't bind DataContext but some custom DP and the collapsing there works fine. However, in this one, the value converter seems to be called only when the SectionHeader != null. When it is null, the control is rendered with empty children.
I thought about not using DataContext here but the control is much cleaner with it.
Why binding to DataContext doesn't work? What do you suggest?
In this case, when DataContext is null, binding will use value specified in TargetNullValue property. So simply set TargetNullValue=Collapsed and you're good to go :
Visibility="{Binding TargetNullValue=Collapsed}"
Reference : [How to Set TargetNullValue to Visibility.Collapsed in Binding]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With