ListView Databinding UI not update with data change

lihuipeng49lihuipeng49 Member ✭✭✭

I use the databinding with the ListView.
When the app runs, there is 3 items in ListView.
I invoke the function 'Entry_Completed' to add the 4th item to the ListView.
I trace the function run over, but there is no 4th item add to the ListView on UI.

MainPage.xaml

        <ListView x:Name="listView" ItemsSource="{Binding}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                            <StackLayout Padding="0" Spacing="0" Orientation="Vertical">
                                <StackLayout Padding="0" Spacing="10" Orientation="Horizontal">
                                <Label Text="{Binding MessageNumber}" FontSize="Micro" TextColor="Silver" />
                                <Label Text="{Binding Nickname}" FontSize="Micro" TextColor="Silver" />
                                <Label Text="{Binding Ip}" FontSize="Micro" TextColor="Silver" />
                                </StackLayout>
                            <Label Text="{Binding Message}" FontSize="Medium" />
                            </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

MainPage.xaml.cs

            public partial class MainPage : ContentPage
            {
                public IList<ChatMessage> chatMessages;

                public MainPage()
                {
                    InitializeComponent();

                    chatMessages = new List<ChatMessage>();
                    for (int i = 0; i < 3; i++)
                    {
                        var msg = new ChatMessage();
                        msg.Nickname = "我的六字昵称";
                        msg.Ip = "127.0.0.1";
                        msg.Message = "这是一条聊天信息";
                        msg.MessageNumber = i + 1;
                        chatMessages.Add(msg);
                    }

                    this.BindingContext = chatMessages;
                }

                private void Entry_Completed(object sender, EventArgs e)
                {
                    var msg = new ChatMessage();
                    msg.Nickname = "我的六字昵称";
                    msg.Ip = "127.0.0.1";
                    msg.Message = sendText.Text;
                    msg.MessageNumber = 100;
                    chatMessages.Add(msg);
                }
            }

Best Answer

Answers

  • jddjdd USMember ✭✭✭

    Hello, I use an observable collection but the UI doesn't update. What I am missing?
    XAML:

                   <Picker BindingContext="{viewModels:SightPageViewModel}"
                            Title="stars"   
                            WidthRequest="120" 
                            Margin="0"
                            ItemsSource="{Binding Stars, Mode=TwoWay}"
                            SelectedIndex="0"
                            IsVisible="{Binding Source ={x:Reference PickerBodyType},
                                               Path=SelectedIndex,
                                               Converter= {StaticResource selectedItemToBool},
                                               ConverterParameter=3}" />
    

    ViewModel:

        class SightPageViewModel : BaseViewModel
        {
            public ObservableCollection<string> Stars { get; private set; }
    
            public SightPageViewModel()
            {
                ObservableCollection<String> Stars = new ObservableCollection<string>();
                Stars.Add(App.StarsNames[32349 ].Designation(System.Globalization.CultureInfo.CurrentCulture));
                Stars.Add(App.StarsNames[30438 ].Designation(System.Globalization.CultureInfo.CurrentCulture));
            }
        }
    
  • jddjdd USMember ✭✭✭

    Solved : The ItemsSource has to be of type IList:

    public IList<string> Stars { get; private set; }
    
Sign In or Register to comment.