Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Visiblity Binding to Boolean Expression with multiple Variables

Tags:

wpf

xaml

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.

like image 704
Stacker Avatar asked Feb 20 '17 11:02

Stacker


People also ask

How to bind visibility of XAML button control to Boolean value?

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

How to bind visibility of WPF elements to a property?

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

What is the difference between Boolean visibility and visibility?

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?

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!


1 Answers

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>
like image 165
mm8 Avatar answered Oct 13 '22 00:10

mm8