How to access the ListView in Viewcell c# code behind ?

Actually I want to bind my IValueConverter with the StackLayout the code behind is

There's my ListView in CommentsPage i.e.,
public class CommentsPage : ContentPage
{

_commentsList.ItemTemplate = new DataTemplate(typeof(CommentsCell));

}

Also there's my CommentsCell that's another class .

public class CommentsCell : ViewCell
{

        ContentView mainLayout = new ContentView
        {

            Padding = new Thickness(1,0,1,0),
            BackgroundColor = Color.White,
            HorizontalOptions = LayoutOptions.StartAndExpand,
            VerticalOptions  = LayoutOptions.StartAndExpand,
            WidthRequest = _screenWidth
        };
        mainLayout.SetBinding(VisualElement.BackgroundColorProperty, new Binding(".", BindingMode.Default, new isFirstOrLastColorConverter(),
            MyItemsListView));

}
Now the question is how to access the following binding there in CommentsCell Class ?
mainLayout.SetBinding(VisualElement.BackgroundColorProperty, new Binding(".", BindingMode.Default, new isFirstOrLastColorConverter(),
MyItemsListView));

Best Answer

  • AsfendYarAsfendYar US ✭✭
    edited December 2016 Accepted Answer

    By default the ListView is Private and it can't be accessible in your viewCell so first of all make this ListView public then you need to declare your ListView as static like this
    public static ListView _commentsList;
    NO access your listview in your CustomCell
    mainLayout.SetBinding(VisualElement.BackgroundColorProperty, new Binding(".", BindingMode.Default,
    new isFirstOrLastColorConverter(),
    CommentsPage._commentsList));

Answers

  • AsfendYarAsfendYar USMember ✭✭
    edited December 2016 Accepted Answer

    By default the ListView is Private and it can't be accessible in your viewCell so first of all make this ListView public then you need to declare your ListView as static like this
    public static ListView _commentsList;
    NO access your listview in your CustomCell
    mainLayout.SetBinding(VisualElement.BackgroundColorProperty, new Binding(".", BindingMode.Default,
    new isFirstOrLastColorConverter(),
    CommentsPage._commentsList));

  • SteveShaw.5557SteveShaw.5557 USMember ✭✭✭
    edited June 2018

    IMO, making member _commentsList static is asking for trouble later. Probably won't cause trouble in this case, but not a good habit. (Can't have two instances of class without conflict, because static only exists once. Static holds on to reference, so will prevent garbage collection unless you remember to clear it in OnDisappearing.)

    So a better answer is:
    "If cell really needs to know ListView, pass in the ListView as a parameter to constructor" [which makes initializing it more complicated; has to be done in code behind of page containing ListView"],

    or best answer:
    "CommentsCell shouldn't know about ListView - binding should be done in code behind of page containing ListView; that is the one place that knows about both the ListView and the desired custom cell."

    Sorry, don't have the code for that at my fingertips, but it is the right principle, as starting place to search for good solution.

  • AndreiMisiukevich_AndreiMisiukevich_ USMember ✭✭✭✭✭

    this.Parent as ListView

Sign In or Register to comment.