Bind the true or false in the listview to get the image

Vasanthakumar M 251 Reputation points
2021-04-29T04:52:14.35+00:00

Hi Experts,

            How i can bind the true or false in the listview . 
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,254 questions
{count} votes

Accepted answer
  1. JessieZhang-MSFT 7,706 Reputation points Microsoft Vendor
    2021-04-29T06:05:04.6+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    How i can bind the true or false in the listview .

    Do you want to bind a bool variable to the item of a listview,right?

    Take the official sample ListView/CustomCells/ for exmaple , you can do like this:

    1.Define a bool variable(e.g. IsVisible) in the item model (VeggieViewModel.cs)

    public class VeggieViewModel  
    {  
        public string Name { get; set; }  
        public string Type { get; set; }  
        public string Image { get; set; }  
    
    
        // add a bool variable IsVisible  
        public bool IsVisible { get; set; }  
    }  
    

    2.init value for the bool variable when creating new model:

    MainViewXaml.cs

    public partial class MainViewXaml : ContentPage  
    {  
        public ObservableCollection<VeggieViewModel> veggies { get; set; }  
        public MainViewXaml ()  
        {  
            InitializeComponent();  
    
            veggies = new ObservableCollection<VeggieViewModel> ();  
            veggies.Add (new VeggieViewModel{ Name="Tomato", Type="Fruit", Image="tomato.png",IsVisible=true});  
            veggies.Add (new VeggieViewModel{ Name="Romaine Lettuce", Type="Vegetable", Image="lettuce.png",IsVisible= false});  
            veggies.Add (new VeggieViewModel{ Name="Zucchini", Type="Vegetable", Image="zucchini.png",IsVisible= true});  
            lstView.ItemsSource = veggies;  
        }  
    }  
    

    3.In MainViewXaml.cs.xaml, if we want to bind this bool variable to the property IsVisible for a Image,we can do like this:

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

    The whole xaml code :

    <ContentPage.Content>  
        <ListView  x:Name="lstView" RowHeight="60">  
            <ListView.ItemTemplate>  
                <DataTemplate>  
                    <ViewCell>  
                        <StackLayout Orientation="Horizontal" HorizontalOptions="Fill" BackgroundColor="Olive">  
                            <StackLayout Orientation="Vertical">  
                                <Label Text = "{Binding Name}" FontSize="24" AbsoluteLayout.LayoutBounds="0.25, 0.25, 400, 40"/>  
                                <Label Text = "{Binding Type}" AbsoluteLayout.LayoutBounds="50, 35, 200, 25"/>  
                            </StackLayout>  
                            <Image Source="{Binding Image}"  IsVisible="{Binding IsVisible}"  HorizontalOptions="End" AbsoluteLayout.LayoutBounds="250.25, 0.25, 50, 50 "/>  
                        </StackLayout>  
                    </ViewCell>  
                </DataTemplate>  
            </ListView.ItemTemplate>  
        </ListView>  
    </ContentPage.Content>  
    

    Best Regards,

    Jessie Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful