Why isn't my UWP ListView showing added items in Win-Universal-App using C#?

45 Views Asked by At

In app on UWP ListView doesn't show items (however, when program was launched first time, listview showed added items)

xaml of listview:

    <!-- Expense List -->
                    <StackPanel Grid.Row="3" Orientation="Vertical" Padding="10">
                        <ListView Name="ExpensesView" Margin="10" Header="Expense" ItemsSource="{x:Bind ViewModel.Expenses, Mode=OneWay}">
                            <ListView.ItemTemplate>
                            <DataTemplate x:DataType="model:Expense">
                                <StackPanel>
                                    <TextBlock Text="{x:Bind Name}" />
                                    <TextBlock Text="{x:Bind Amount}" />
                                    <TextBlock Text="{x:Bind Category}" />
                                    <Button Content="Delete" Click="DeleteExpense_Click" DataContext="{x:Bind}" />
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </StackPanel>

binding it to a ObservableCollection in file View:

public class MainPageViewModel
    {
        public ObservableCollection<Expense> Expenses { get; set; }
        
        public MainPageViewModel()
        {
            Expenses = new ObservableCollection<Expense>();
        }

        public void AddExpense(Expense expense)
        {
            Expenses.Add(expense);
        }
        
    }

datatype class:

    public class Expense
    {
        public string Name { get; set; }
        public double Amount { get; set; }
        public string Category { get; set; }

I input expense in text fields (working fine in the debug mode). But the listview doesn't show this expenses on the page (however, when program was launched first time, listview showed added expenses) code for the event handler:

    private void AddExpense_Click(object sender, RoutedEventArgs e)
        {
            string expenseName = ExpenseNameTextBox.Text;
            double expenseAmount = double.Parse(ExpenseAmountTextBox.Text);
            string expenseCategory = ((ComboBoxItem)ExpenseCategoryComboBox.SelectedItem).Content.ToString();

            Expense expense = new Expense(expenseName, expenseAmount, expenseCategory);
           
            ViewModel.Expenses.Clear();
            ViewModel.AddExpense(expense);
            SaveDataAsync();

            string message = $"Expense Added!\n\nName: {expense.Name}\nAmount: {expense.Amount}\nCategory: {expense.Category}";
            MessageDialog dlg = new MessageDialog(message);
            dlg.ShowAsync();
        }

I saw potential fixes for the problem, but it didn't help me and I couldn't identify the problem, so any help would be greatly appreciated! thanks.

Some suggested to clear collection before adding new items to not break the bounding - it is what implemented right now, but it didn't work.

listview should show all added items.

Update Implemented INotifyPropertyChanged interface for MainPageViewModel (where ObservableCollection is stored) which might help with binding after adding or deleting elements:

public class MainPageViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<Expense> expenses;
        public ObservableCollection<Expense> Expenses
        {
            get { return expenses; }
            set
            {
                if (expenses != value)
                {
                    if (expenses != null)
                        expenses.CollectionChanged -= Expenses_CollectionChanged;

                    expenses = value;

                    if (expenses != null)
                        expenses.CollectionChanged += Expenses_CollectionChanged;

                    OnPropertyChanged(nameof(Expenses));
                }
            }
        } 

        public MainPageViewModel()
        {
            Expenses = new ObservableCollection<Expense>();
        }

        public void AddExpense(Expense expense)
        {
            Expenses.Add(expense);
            UpdateSavings();
        }
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new 
              PropertyChangedEventArgs(propertyName));
        }
}

screenshot Here in expenses should be added element, but it is not working

0

There are 0 best solutions below