1

I'm using XF list view, and want to binding the whole object and then use converter to format the data based on some conditions, but object binding does not work anymore in XF. Test is on XF 2.5.0.91635, and android support library 26.1.0.1. First, when do not use item template, the list view is rendered correctly, though content is showing default object string:

<ListView x:Name="lv"></ListView>

enter image description here

But when using item template, nothing is show, though the number of items are indeed added in list view, and item selected event is fired correctly. Also, debug outputs following messages:

        <ListView x:Name="lv">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Label Text="{Binding}"></Label>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

enter image description here

[0:] Binding: Models.DataItem can not be converted to type 'System.String'
[0:] Binding: Models.DataItem can not be converted to type 'System.String'
[0:] Binding: Models.DataItem can not be converted to type 'System.String'
[0:] Binding: Models.DataItem can not be converted to type 'System.String'
[0:] Binding: Models.DataItem can not be converted to type 'System.String'

And when I add converter to binding expression, the convert function always receives null in its arguments.

add binding code:

List<DataItem> list = new List<DataItem>();
        for(int i = 0; i < 10; i++)
        {
            list.Add(new DataItem
            {
                N = "L " + i.ToString(),
                C = i + 1,
            });
        }
        lv.ItemsSource = list;
2
  • Not entirely sure what is happening, but do I understand correctly that you define it like this <Label Text="{Binding}"></Label>? Normally you would define it like <Label Text="{Binding PropertyOnModel}"></Label> where PropertyOnModel is a property on the data bound model, or at least <Label Text="{Binding .}"></Label> to bind to the entire model object. Does that resolve the issue? Commented Nov 20, 2017 at 13:55
  • Yeah, binding to property works, but I intentionally want to bind to entire object and pass it to a converter, and it does not work anymore, the same issue with {Binding .}, or {Binding Path=.}. Commented Nov 20, 2017 at 13:58

2 Answers 2

1

And when I add converter to binding expression, the convert function always receives null in its arguments.

If you want to bind to entire object you will need to modify the DataItem like this:

public class DataItem
{
    public String N { get; set; }
    public int C { get; set; }

    //use an Item property to reference itself
    public DataItem Item { get { return this; } }
}

Then you can use it in your ListView together with converter:

<ListView  x:Name="lv" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Label Text="{Binding Item,Converter={StaticResource YourConverter}}" ></Label>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
 </ListView>
Sign up to request clarification or add additional context in comments.

2 Comments

That's brilliant, never thought about this way, thanks! How do you get the idea, Do you have any source or docs of this workaround?
Sorry no doc about this, just out of experience.
0

Correct way to bind is "Binding collection to ListView" and by providing ItemSource for list:

For example:

private ObservableCollection<Models.DataItem> _dataList;
public ObservableCollection<Models.DataItem> DataList
{
    get { return _dataList; }
    set
    {
        _dataList = value;
        RaisePropertyChanged(() => DataList); // you can use your own property notification mechanism here.
    }
}

Then after setting binding context to your ViewModel:

<ListView ItemsSource="{Binding DataList}" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Label Text="{Binding . }" />
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Or if you want to display properties from following DataItem:

public class Model.DataItem
{
    public string Property1 {get; set; } 
    public string Property2 {get; set; }
    public string Property3 {get; set; }
}

use

<ViewCell>
    <Label Text="{Binding Property1 }" />
    <Label Text="{Binding Property2 }" />
    <Label Text="{Binding Property3 }" />
</ViewCell>

Hope this helps

7 Comments

Thanks for posting, but not what I'm asking, I know how to bind to collection and property binding. The issue here is xamarin runtime somehow cannot handle object binding, even with converters - it always receives null.
Can you post your "actual" code including viewmodel and converter code. Because your question doesn't make proper sense.
I just added the binding code. The question makes perfect sense if you actually tested it. The issue is there without converter involved, i just used converter to test further to confirm there is the issue.
Following my answer, as you are setting ItemSource in code behind, you can display DataItem properties (N, C). You still didn't add converter code (would be much easier to find the problem if I know how you are converting objects). You can implement ToString() to get proper string conversion though.
That's the problem, binding still fails after adding ToString to the object. You can use a stub converter and print out value parameter, you will notice it is null.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.