question

MarkConnelly-6630 avatar image
0 Votes"
MarkConnelly-6630 asked MarkConnelly-6630 commented

Binding IsVisible property ListView Datatemplate to ViewModel

Hi guys,

Got a Datatemplate list that I want to toggle the buttons on and off depending on something being shown in the view model.


         <ListView IsVisible="{Binding IsDisplayMenu}" IsRefreshing="{Binding IsRefreshing}" Margin="10" x:Name="ListView" ItemsSource="{Binding FoodItemList}" HasUnevenRows="True" ItemSelected="listView_ItemSelected">
             <ListView.ItemTemplate>
                 <DataTemplate>
                     <ViewCell x:Name="viewCell">
                         <StackLayout Margin="5" x:Name="ListViewStackLayout">
                             <Grid>
                                 <...>
                                         <StackLayout Grid.Row="0" Grid.Column="2" HorizontalOptions="End">
                                             <Button
                                                  x:Name="addButton"
                                                  Padding="10"
                                                  Text="ADD"
                                                  CornerRadius="10"  
                                                  BackgroundColor="{DynamicResource PrimaryColor}"
                                                  TextColor="{DynamicResource PrimaryButtonTextColor}"
                                                  FontSize="24"                                                                                                                                      
                                                  FontFamily="FetteEng"
                                                  FontAttributes="Bold"
                                                  BindingContext="Binding Source={RelativeSource AncestorType={x:Type ViewModels:MenuPageViewModel}}, Path=AddToBasketCommand}"   
                                                  CommandParameter="{Binding .}"
                                                  IsVisible="{Binding IsAddVisible}"/>                                                                                                                                                                                                                     

                                             <ImageButton Source="chevronblack.png"
                                                          IsVisible="{Binding IsChevronVisible}"></ImageButton>
                                           </StackLayout>


  private void ChangeButton(string menuItemName)
         {
             // If the header is pizzas or flatbread.
             if(menuItemName == "Pizzas" || menuItemName == "Flatbread")
             {
                 this.IsChevronVisible = true;
                 this.IsAddVisible = false;
             }
             else
             {
                 this.IsChevronVisible = false;
                 this.IsAddVisible = true;
             }
         }

I've set up bindings on each of the IsVisible properties to toggle them on and off but they aren't attached to the VM for some reason. Any help would be appreciated.

dotnet-csharpdotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

alessandrocaliaro avatar image
0 Votes"
alessandrocaliaro answered MarkConnelly-6630 commented

IsAddVisible should be a FoodItemList's property. It seems a ViewModel's property.
Do you have IsAddVisible in the FoodItemList's Model?

· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I do not have that, let me add it in and run and I'll let you know.

0 Votes 0 ·

Added to the viewmodel, it seems to be working on the first time i load the page. However, I want to make the properties interchangeable. So when I select something on the page, I want IsAddVisible to turn false and IsChevronVisible to turn true and reflect those changes on the page. However, it's only being set once when I load the page up and not changing. Any ideas?

0 Votes 0 ·

Do you set IsAddVisible inside the Model? Do you Implement INotifyPropertyChanged?

0 Votes 0 ·

Hey, thanks for getting back to me. I set it in the VMs construction but it's a property in the model. I've not implemented INotifyProperyChanged, would that not have to be on the .xaml.cs?

0 Votes 0 ·
Show more comments