0

I have a Button, and I want the button to be visible only when Condition A == true and Condition B == true.

Now, my viewModel already have two Property whose return type is boolean.
So is it possible to achieve it with XAML Binding

Prophet
  • 32,350
  • 22
  • 54
  • 79
FendouHui
  • 47
  • 5

1 Answers1

0

If you want your button to be visible depending on two properties then in this case it would be better you make a Visibility Property in your Viewmodel and bind that property to Button Visibility Property in Xaml.

For example :-

In Viewmodel create Visibility property -

    private Visibility _visBtn = Visibility.Collapsed;

    public Visibility VisBtn 
    {
        get { return _visBtn ; }
        set
        {
            _visBtn = value;
            RaisePropertyChanged("VisBtn "); // INotifyPropertyChanged Implemented 
        }
    }

You have to just set this property according to your logic. like :-

If(Condition A == true && Condition B == true)
    VisBtn = Visibility.Visible;

Now Bind this property to Button In Xaml Like -

<Button Content="My Button" Visibility="{Binding VisBtn }" />

Note :- Make sure that you have implemented INotifyPropertyChanged In your Viewmodel and Your Xaml Page DataContext is set to corresponding Viewmodel properly.

Second Case :- If you want to set your button Visibility on basis of single bool property then you can implement BooleanToVisibility Converter this converter map Boolean property to Visibility type.

Then How to implement boolean to visibility Converter help you.

Community
  • 1
  • 1
loop
  • 9,002
  • 10
  • 40
  • 76