Bindable Custom Cell Property Doesn't change element visibility

moszoromoszoro PLMember

Hello Guys,

I have custom cell that has few bindings. One of them is Visibility of selected "Ticker" image. Binding is working well on cell creation, but when i capture TappedEvent and change item.selected property to 'true" nothing happens visually :(

Please help.

ListView + customCell:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="OddayeV2.CategorySelect">
    <ContentPage.Content>
        <ListView x:Name="listView" RowHeight="100" SeparatorVisibility="None">
            <ListView.Header>
                <StackLayout Orientation="Horizontal">
                    <Label Margin="20" Text="Wybierz kategorię swojego profilu, abyśmy mogli wyświetlić go osobom, które są zainteresowane tym co robisz (możesz zaznazyć max. 3 kategorie)" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontSize="14" TextColor="Black" BackgroundColor="White" />
                </StackLayout>
            </ListView.Header>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <AbsoluteLayout>
                            <Image Source="{Binding image}" Aspect="AspectFill" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="XProportional, YProportional, SizeProportional" />
                            <StackLayout AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="XProportional, YProportional, SizeProportional" IsVisible="{Binding selected}">
                                <Image Source="Tick.png" HorizontalOptions="EndAndExpand" />
                            </StackLayout>
                            <StackLayout Margin="20" Orientation="Vertical" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="XProportional, YProportional, SizeProportional">
                                <Label Text="{Binding title}" TextColor="#f35e20" FontSize="20" />
                                <Label Text="{Binding subtitle}" HorizontalOptions="EndAndExpand" TextColor="#503026" FontSize="14" />
                            </StackLayout>
                        </AbsoluteLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

Category Class:

    public class Category
    {
        public string title { get; set; }
        public string subtitle { get; set; }
        public string image { get; set; }
        public bool selected { get; set; }
    }

ItemTapped eventHandler:

        void OnItemTapped(object sender, ItemTappedEventArgs e)
        {
            var item = e.Item as Category;

            item.selected = !item.selected;
        }

Best Answer

  • moszoromoszoro PL
    Accepted Answer

    Ok I resolved this problem implementing INotifyPropertyChanged interface to Model

        public class Category : INotifyPropertyChanged
        {
            public string title { get; set; }
            public string subtitle { get; set; }
            public string image { get; set; }
            private bool _selected;
            public bool selected { get { return _selected; } set { _selected = value; OnPropertyChanged("selected");} }
            public int id { get; set; }
    
            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(string nameOfProperty)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(nameOfProperty));
            }
        }
    

Answers

  • moszoromoszoro PLMember
    Accepted Answer

    Ok I resolved this problem implementing INotifyPropertyChanged interface to Model

        public class Category : INotifyPropertyChanged
        {
            public string title { get; set; }
            public string subtitle { get; set; }
            public string image { get; set; }
            private bool _selected;
            public bool selected { get { return _selected; } set { _selected = value; OnPropertyChanged("selected");} }
            public int id { get; set; }
    
            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(string nameOfProperty)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(nameOfProperty));
            }
        }
    
Sign In or Register to comment.