0
votes

I have a ListView that utilizes a DataTemplate. Inside the data template I have a few StackLayouts. The ListView ItemsSource is set to a list of "Category"(Category is my class), and the IsVisible property of one of those StackLayouts is bound to Category.IsVisible. ClassId is also bound, so on tap gesture, I figure out which Category item is the item behind the ListView item and set it's IsVisible. That does not work though, and the StackLayout remains visible

<ListView  
     SeparatorColor="#888888"
     SeparatorVisibility="Default"
     BackgroundColor="White"  
     SelectionMode="None"
     HasUnevenRows="True" 
     x:Name="mainListView">

     <ListView.Header>
         <ContentView 
                Padding="0,5">

                    <Label 
                        Margin="0,0,0,15"
                        FontSize="20" 
                        TextColor="#FF4081"            
                        Text="WINDOW CLEANING"/>
                    <!--TextColor="#FF4081"-->
                    
                </ContentView>
            </ListView.Header>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.ContextActions>
                            <MenuItem 
                                Clicked="OnDelete" 
                                CommandParameter="{Binding .}" 
                                Text="Delete" 
                                IsDestructive="True" />
                        </ViewCell.ContextActions>
                        <StackLayout
                            ClassId="{Binding Id}"
                            Orientation="Vertical">
                            <StackLayout.GestureRecognizers>
                                <TapGestureRecognizer
                                    Tapped="Category_Tapped"
                                    NumberOfTapsRequired="1" />
                            </StackLayout.GestureRecognizers>
                            <StackLayout 
                                BackgroundColor="#e0e0e0"
                                Orientation="Horizontal"
                                VerticalOptions="FillAndExpand" 
                                Padding="5" 
                                Grid.Column="0">

                                <Image
                                    Source="{Binding ImageSource}" />

                                <Label 
                                    Margin="10,0,0,0"
                                    VerticalOptions="Center"
                                    HorizontalOptions="StartAndExpand"
                                    TextColor="#FF4081"
                                    FontSize="17"  
                                    Text="{Binding Name}" />

                                <Label 
                                    Margin="0,0,10,0"
                                    VerticalOptions="Center"
                                    HorizontalOptions="End"
                                    TextColor="DarkCyan"
                                    Font="Bold, 20" 
                                    Text="{Binding ShortedAmount}" />
                            </StackLayout>
                            <StackLayout 
                                Orientation="Horizontal"
                                IsVisible="{Binding IsVisible, Mode=OneWayToSource}">

                                <Label 
                                    HorizontalOptions="StartAndExpand"
                                    Margin="5,10,0,20"
                                    TextColor="DarkCyan"
                                    Font="Bold, 16"
                                    Text="{Binding Text}"/>

                                <Label 
                                    HorizontalTextAlignment="End"
                                    HorizontalOptions="End"
                                    Margin="0,10,10,20"
                                    TextColor="DarkCyan"
                                    Font="Bold, 16"
                                    Text="{Binding Amount}"/>
                                
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

Code behind snippets:

    mainListView.ItemsSource = CategoryFactory.Categories;

    private void Category_Tapped(object sender, EventArgs e)
    {
        string id = ((StackLayout)sender).ClassId;
        id = id.Replace("CAT", "");
        int i = Convert.ToInt32(id);
        CategoryFactory.Categories[i].IsVisible = !CategoryFactory.Categories[i].IsVisible;
    }

I think you get the idea. I want to create an expandable list in runtime

1
why are you using OneWayToSource? - Jason
Oh, just to try. It isn't working without that anyway, or with twoway, or with default... - Nikolai Frolov
does your Category class implement INotifyPropertyChanged? - Jason
Nope, Didn't knew about that interface... - Nikolai Frolov
without it your UI will have no way of knowing that a property has been updated - Jason

1 Answers

1
votes

your model/viewmodel class must implement INotifyPropertyChanged in order for the UI to be notified of changes to the properties