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.
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.
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.
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.
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.
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.
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>
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