0

I have a ListView with a data template. I am trying to have a custom component that supports binding for the content of the data template.

Here is the ListView in the page:

<ListView ItemsSource="{Binding List}">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="entities:ListItem">
            <ViewCell>
                <components:ListItemView ListItem="{Binding}" />
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

And this is the ListItemView declaration:

public partial class ListItemView : StackLayout
{

    public static readonly BindableProperty ListItemProperty
        = BindableProperty.Create(
                    nameof(ListItem), typeof(ListItem), typeof(ListItemView), null,
                    defaultBindingMode: BindingMode.TwoWay,
                    propertyChanged: ListItemPropertyChanged);

    static void ListItemPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var view = (ListItemView)bindable;
        view.ListItem = (ListItem)newValue;
    }


    public ListItem ListItem
    {
        get => (ListItem)GetValue(ListItemProperty);
        set
        {
            SetValue(ListItemProperty, value);
            if (_viewModel != null) // never hits this break point
                _viewModel.ListItem = value;
        }
    }

I had a breakpoint on the line with the comment. This breakpoint was never hit. ListItemView however does get initialized and created.

Update

I tried a simple demo to ensure the issue was in the binding,

<StackLayout Padding="5">
    <Label Text="{Binding Demo.Title}" />
    <components:CheckListView ListItem="{Binding Demo}" />
</StackLayout>

The above code was outside the list view and I am able to see the title. The breakpoint is still not hit.

Neville Nazerane
  • 6,622
  • 3
  • 46
  • 79
  • Not sure if this will work but it might be worth a try. I've learned the best way to bind to the BindingContext object is ``. Even if this won't help I believe it's better this way since this makes it clear to other readers that you're specifically targetting the BindingContext instead of just forgotten to put in what you want to bind to. –  Feb 28 '19 at 09:10
  • well the code does not crash, but output still the same. Regarding it being better, I don't see it. if you forget something like that there is always type of safety to take care of it. I am not seeing how adding a character that seems to be random would help in any way – Neville Nazerane Feb 28 '19 at 11:03
  • Yeah it's a bit of personal preference. If I read code written by another programmer that said `{Binding .}` it would tell me this was done specifically to bind to the bindingcontext object itself. The `{Binding}` feels much more like a wildcard to me, it doesn't communicate a purpose, so to speak. But it's only a readability thing, so as said it's up to personal preference. Though a good example would be that you probably wouldn't have gotten the answer of Juansero29 if my notation was used. –  Feb 28 '19 at 13:13
  • One way for `ListItemView` to get initialized and created but not hit the break point is when the list is empty. Can you share the code where the list is created and where items are added? –  Feb 28 '19 at 13:20
  • no list isn't empty. that's what i meant by "it gets initialized and created" the constructor is hitting n times and n items are being displayed – Neville Nazerane Mar 01 '19 at 05:19
  • Ah I read that wrong, but my line of thinking still remains that the problem is somewhere else. I've created a test project and inserted your code (of course I had to write some parts not mentioned here, but nothing special) and everything works as it should (I'm hitting the break point, and my ListItem's get displayed the way I defined in ListItemView). Please take a look at this: https://stackoverflow.com/help/mcve. If you did already make a minimal sample project, could you post it github and provide the link? –  Mar 01 '19 at 08:51
  • @Knoop i just made the git repo here: https://stackoverflow.com/questions/54966384/setting-up-a-simple-component-with-data-binding – Neville Nazerane Mar 03 '19 at 07:09

0 Answers0