Zany
Zany

Reputation: 1015

Xamarin c# ListView binding not working , equivalent xaml is working

As the heading states my ListView binding from within xaml works , but doesnt work in c#.

Here's the code snippet: Xaml

<ListView ItemsSource="{Binding Records}">
                <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding}">

                </TextCell>
                       </DataTemplate>       
                </ListView.ItemTemplate>
            </ListView>

C#

            ListView lv = new ListView();
            lv.ItemsSource = ClassVMInstance.Records;
            var dt = new DataTemplate(typeof(TextCell));
            dt.SetBinding(TextCell.TextProperty, new Binding("Records"));
            lv.ItemTemplate = dt;

ClassVMInstance is an instance of my ViewModel.

Records is an ObservableCollection<string>

The xaml version works fine , it shows the text , but the c# version just has empty list elements without the text.

(I tested this on the same page with 2 listview one xaml and the other c# , only the xaml one shows the text , but the c# version just has same number of list items but empty)

I am to believe that the itemssource property is working properly in code but the binding is not can someone help me out.

Upvotes: 0

Views: 467

Answers (1)

Jason
Jason

Reputation: 89204

try this

dt.SetBinding(TextCell.TextProperty, new Binding("."));

Upvotes: 2

Related Questions