Anton Duzenko
Anton Duzenko

Reputation: 2596

Xamarin.Forms binding not working

I can't get the new data show on the screen.

C# code

public partial class MainPage : ContentPage {
    public class PhaseDetails {
        public List<string> Chats {
            get; set;
        }
    }

    public new PhaseDetails BindingContext => (PhaseDetails) base.BindingContext;

    public MainPage() {
        InitializeComponent();
        base.BindingContext = new PhaseDetails {
            Chats = new List<string>( new string[] { "qwe" } ),
        };
        Task.Run( () => new List<string>( new string[] { "1", "2", "3" } )).ContinueWith( (task) => Device.BeginInvokeOnMainThread( () => {
            BindingContext.Chats = task.Result;
            OnPropertyChanged( null );
        } ) );
    }
}

XAML

<StackLayout>
    <ListView ItemsSource="{Binding Chats}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Padding="6" BackgroundColor="Aquamarine">
                        <Label Text="Service Area"/>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>

Expected: three rows in the list view. But it only shows one. What could be the problem?

Upvotes: 0

Views: 1973

Answers (2)

mark333...333...333
mark333...333...333

Reputation: 1360

You can try this simple project solution available on this github page. It uses Data binding Mode=TwoWay from ViewModel to View.

enter image description here

Upvotes: 1

Anton Duzenko
Anton Duzenko

Reputation: 2596

This helped:

        public class PhaseDetails {
...
            public event PropertyChangedEventHandler PropertyChanged;
            void OnPropertyChanged([CallerMemberName] string propertyName = null) {
                PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( propertyName ) );
            }

The call OnPropertyChanged from property setter.

Upvotes: 0

Related Questions