Shruti Dutt
Shruti Dutt

Reputation: 37

Xamarin.forms - BoxView in Customized ViewCell

I want to set VisibleProperty of BoxView as false for the last element of list which is defined in a viewcell containing stacklayout (grid+boxview). Is there any way to make it false so that last element wont contain boxview separator line?

Upvotes: 1

Views: 269

Answers (1)

nevermore
nevermore

Reputation: 15786

Solution:

You can add a bool property isShow to your viewModel and use this property to control whether the boxView is visible or not.

public class MyViewModel
    {
        //use this property to control whether the boxView is visible
        public bool isShow { get; set; }
    }

And then binding this property to the boxView in your customCell By boxOne.SetBinding(BoxView.IsVisibleProperty, new Binding("isShow")):

public CustomCell()
    {
        //instantiate each of our views

        var grid = new Grid();
        var horizontalLayout = grid;
        grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

        var boxOne = new BoxView { BackgroundColor = Color.Purple };
        var boxTwo = new BoxView { BackgroundColor = Color.AliceBlue };    
        grid.Children.Add(boxOne, 0, 0);        
        grid.Children.Add(boxTwo, 1, 1);

        //Binding here
        boxOne.SetBinding(BoxView.IsVisibleProperty, new Binding("isShow"));

        View = horizontalLayout;                 
     }

At last , when you creating a new instance of MyViewModel, you can set the isShow property true/false to control whether the boxView is visible in your cell.

    public MainViewCode()
    {
        myCollection = new ObservableCollection<MyViewModel>();
        ListView lstView = new ListView();  
        lstView.ItemTemplate = new DataTemplate(typeof(CustomCell));

        myCollection.Add(new MyViewModel { isShow = true });
        myCollection.Add(new MyViewModel { isShow = false });
        lstView.ItemsSource = myCollection;

        Content = lstView;
    }

Upvotes: 1

Related Questions