IsEnabled False If Binding.Source is not available
Asked Answered
T

2

7

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.

Teat answered 21/8, 2011 at 19:46 Comment(0)
C
10

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.

Coatbridge answered 21/8, 2011 at 20:5 Comment(0)
E
4

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>
Evenfall answered 21/8, 2011 at 20:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.