I have two Booleans I want to show Image based on their value, something like this :
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Window.Resources>
<Image Visibility="{Binding (Boolean1 && Boolean2),Converter={StaticResource BooleanToVisibilityConverter}}" />
Notice Boolean1 and Boolean2 expression.
Some instructions on how to bind the visibility of a XAML button control to a boolean value in a ViewModel class. 1. Create a new WPF application. 2. Add the View Model class. Right click your project folder and select Add > New Item > Class. Name your class MainWindowViewModel.cs: In your ViewModel class include the property to get the ...
Binding the visibility of WPF elements to a property 1 Step 1: Create a new WPF Application. 2 Step 2: Modify the MainWindow.xaml to create a User control. 3 Step 3: Create the ViewModel classes. 4 Step 4: Update the MainWindow.xaml to bind to this visibility property. Step 5: Try it! More ...
Visibility has three possible values but a Boolean only has two. This will cover many of the scenarios, but not all. Here is how it would be used. For this example, I have this Person class. ? Here is a simple View for this object. It has a Grid that has a Label and TextBox for each property in the Person object. It also has a CheckBox.
How to set the visibility of your WPF user interface item so that it may be hidden/collapsed or shown as desired Step 1: Create a new WPF Application Step 2: Modify the MainWindow.xaml to create a User control Step 3: Create the ViewModel classes Step 4: Update the MainWindow.xaml to bind to this visibility property Step 5: Try it!
There is no &&
operator defined in XAML but you could bind to several properties and use an IMultiValueConverter
:
<Image>
<Image.Visibility>
<MultiBinding Converter="{StaticResource YourMultiConverter}">
<Binding Path="Boolean1" />
<Binding Path="Boolean2" />
</MultiBinding>
</Image.Visibility>
</Image>
public class YourMultiValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool a = (bool)values[0];
bool b = (bool)values[1];
return a && b ? Visibility.Visible : Visibility.Collapsed;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Or you could use an Image
style with conditions:
<Image>
<Image.Style>
<Style TargetType="Image">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Boolean1}" Value="True" />
<Condition Binding="{Binding Boolean2}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Visibility" Value="Visible" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
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