chung
chung

Reputation: 1133

How to set a control in XAML visible if another control is visible on the same page

I have a card in Xamarin Forms that I want to be visible only if a button is visible as well. For some reason the same IsVisible attribute for the button isn't working for the card, so is there a way for me to say if the button is visible, let the card be visible?

<cor:BindableToolbarItem x:Name="Inside"
            Command="{Binding InsideCommand}"
            IsVisible="{Binding SelectedReading.Inside, Converter={StaticResource valueIsNotNull}}"
            Icon="hi.png"
            Order="Primary" />

That's the button on the toolbar:

<controls:InsideCard 
              BindingContext="{x:Reference Inside}"
              IsVisible="{Binding SelectedReading.Inside, Converter={StaticResource valueIsNotNull}}">
            </controls:InsideCard>

As you can see the IsVisible is the same for both, but it isn't working, so I tried referencing the button inside the card but it doesn't work obviously. How can I do it properly?

Upvotes: 0

Views: 1283

Answers (1)

Nikhileshwar
Nikhileshwar

Reputation: 1684

The BindingContext is set to the Inside element hence the SelectedReading.Inside property will not be available in the Inside object. Hence the binding won't work. Bind the IsVisible property of Inside.

<controls:InsideCard BindingContext="{x:Reference Inside}" IsVisible="{Binding IsVisible}"/>

Or set the source of binding to {x:Reference Inside} in Binding markup itself.

<controls:InsideCard IsVisible="{Binding IsVisible, Source={x:Reference Inside}}"/>

Just to add:

Moreover binding same property to two different Views should work too (If you are setting it in ViewModel). Or also setting Mode as TwoWay for Inside element. For both the cases you must have implemented the NotifyPropertyChanged for the binded property of ViewModel.

    <Label
        x:Name="label1"
        IsVisible="{Binding ViewVisiblity}"
        Text="Hi there"/>
    <Label
        x:Name="label2"
        Text="Hi there2"
        IsVisible="{Binding ViewVisiblity, Mode=TwoWay}"/>

Upvotes: 1

Related Questions