Question

Have a button that I want disabled if a binding value is false or null. Here is was I tried.

<Button Content="Open" IsEnabled="{Binding SearchItem.WFBatchFolderStatus.UserCanOpen, Mode=OneWay, TargetNullValue=false, Converter={StaticResource booleanPassThru}}" />  

I have a case in which SearchItem.WFBatchFolderStatus can be null (and for valid business reasons). If SearchItem.WFBatchFolderStatus is null then I want the button disabled. When SearchItem.WFBatchFolderStatus is null then the converter does not fire. If SearchItem.WFBatchFolderStatus is not null then the converter fires. The converter just returns false if the value is null and otherwise the the value. But the converter never sees a null. When SearchItem.WFBatchFolderStatus is null the button IS enabled (not what I want). If I remove the TargetValue and/or Converter then button is still enabled when SearchItem.WFBatchFolderStatus is null.

Was it helpful?

Solution

The binding fails if a part of the path is null, set the Binding.FallbackValue to false and it should be disabled if WFBatchFolderStatus is null.

OTHER TIPS

How about using a style instead?

<Page.Resources>
    <Style x:Key="SomeStyle" TargetType="{x:Type Button}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding SearchItem.WFBatchFolderStatus.UserCanOpen}" Value="{x:Null}">
                <Setter Property="IsEnabled" Value="False"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Page.Resources>
<Grid>
    <Button Content="Open" Style="{StaticResource SomeStyle}" />
</Grid>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top