0

I am trying to bind the IsVisible property to a StackLayout inside a DataTemplate and I can't make it work.

I have tried this post but in the x:Reference it does not let me put the x:Name of the CollectionView.

<CollectionView x:Name="myCollectionView"
            HorizontalOptions="FillAndExpand"
            ItemsSource="{Binding myBookList}">

  <CollectionView.ItemTemplate>
      <DataTemplate x:Name="dataTemplate">
          <ContentView>
              <StackLayout HorizontalOptions="EndAndExpand"
                           IsVisible="{Binding StackIsVisible}"
                           Orientation="Horizontal">
                  <Grid>
                      <ImageButton Margin="0,0,25,0"
                                   HorizontalOptions="EndAndExpand"
                                   Source="img1"
                                   WidthRequest="25" />
                      <ImageButton Source="img2"
                                   WidthRequest="25"
                                   HorizontalOptions="End" />
                  </Grid>
                  <ImageButton x:Name="myImageButton"
                               HorizontalOptions="EndAndExpand"
                               Source="img3"
                               WidthRequest="25" />
              </StackLayout>
          </ContentView>
      </DataTemplate>
   </CollectionView.ItemTemplate>

The ViewModel and the view are correctly connected since I have other Bindings working but this one does not work for me.

     private bool _StackIsVisible;
     public bool StackIsVisible
     {
             get => _StackIsVisible;
             set
             {
                 _StackIsVisible = value;
                 OnPropertyChanged();
             }
     }
     public ViewModel(){
           StackIsVisible=false;
     }
Cfun
  • 8,442
  • 4
  • 30
  • 62
UyerBit
  • 71
  • 6
  • Take a look on: https://stackoverflow.com/questions/65991141/wpf-binding-upwards-bind-to-view-model-property-inside-nested-uielement/65991459#65991459 – Miamy Feb 02 '21 at 08:33
  • You can use a Reference like in https://stackoverflow.com/a/65991607 no need to specify Name just put `Binding source={x:Reference myCollectionView}` – Cfun Feb 02 '21 at 12:47

1 Answers1

1

You don't have to specify the "Name" keyword when using Reference

<StackLayout IsVisible="{Binding Source={x:Reference myCollectionView},
                                     Path=BindingContext.StackIsVisible}" ...>
Cfun
  • 8,442
  • 4
  • 30
  • 62