Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IsEnabled False If Binding.Source is not available

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.

like image 257
paparazzo Avatar asked Aug 21 '11 19:08

paparazzo


People also ask

Which property is related to data binding?

The source of databinding can be a normal . NET property or Dependency property, however the target property must be a Dependency property. For making binding work properly, both sides of the property must provide a change in notification which will tell the binding to update the target value.

How does binding work in WPF?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.

How to bind a property in XAML?

One-Way Data Binding The following XAML code creates four text blocks with some properties. Text properties of two text blocks are set to “Name” and “Title” statically, while the other two text blocks Text properties are bound to “Name” and “Title” which are class variables of Employee class which is shown below.

What is IsEnabled in WPF?

This property is available on many controls in WPF, including the TextBox and Button. When IsEnabled is False, a control is made inactive—it cannot be clicked or used.


2 Answers

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.

like image 124
H.B. Avatar answered Oct 02 '22 08:10

H.B.


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>
like image 20
Goblin Avatar answered Oct 02 '22 10:10

Goblin