G
G
Georgy Kuznetsov2021-11-19 18:41:52
C++ / C#
Georgy Kuznetsov, 2021-11-19 18:41:52

For what reason is the ListView not updated (I use WPF MVVM, EF Core, SQL Server)?

Let's say there are three files, MainWindowViewModel, TCommand and window.xaml, the development environment itself sees everything up to the types of variables for the fields of the User object (id - string, name - string, age - string, balance - int), but when you click on the ShowAll button Nothing happens :

--- ViewModel.cs---

        public event PropertyChangedEventHandler? PropertyChanged;

        public virtual void CheckChanges([CallerMemberName] string property = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
        }


        public List<User> userlist;

        public List<User> UserList
        {
            get { return userlist; }
            set
            {
                userlist = value;
                CheckChanges();
            }
        }

        public TCommand ShowAllDb_Click
        {
            get
            {
                return new TCommand
                (
                    (obj) =>
                    {
                        UserList = new List<User> { new User { ID = "1", Name = "Joe", Age = "25", Balance = 50 } };
                    }
                );
            }
        }


--TCommand--

using System;
using System.Windows.Input;

namespace WPF_SQLOperator
{
    class TCommand : ICommand
    {
        private Action<object> execute;
        private Func<object, bool> canExecute;

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public TCommand(Action<object> _execute, Func<object, bool> _canExecute = null)
        {
            execute = _execute;
            canExecute = _canExecute;
        }

        public bool CanExecute(object parameter)
        {
            return canExecute == null || canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            execute(parameter);
        }
    }
}


--window.xaml

<Window x:Class="WPF_SQLOperator.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF_SQLOperator"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Title="SQL Operator" Height="735" Width="745" ResizeMode="NoResize">
    <Window.DataContext>
        <local:MainWindowViewModel/>
    </Window.DataContext>
    <Grid>
        <Button Command="{Binding ShowAllDb_Click}" Margin="15,247,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Height="40" Width="333">
            <TextBlock TextAlignment="Center"><Run Text="*Show all"/></TextBlock>
        </Button>
        <ListView ItemsSource="{Binding UserList}" Margin="200,292,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Height="396" Width="349">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="ID" Width="48" DisplayMemberBinding="{Binding ID}" />
                    <GridViewColumn Header="Name" Width="100" DisplayMemberBinding="{Binding Name}" />
                    <GridViewColumn Header="Age" Width="100" DisplayMemberBinding="{Binding Age}" />
                    <GridViewColumn Header="Balance" Width="100" DisplayMemberBinding="{Binding Balance}" />
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Georgy Kuznetsov, 2021-11-19
@JoeSmith3100

For the umpteenth time, for the same rake: I forgot to implement the INotifyPropertyChanged interface in the ViewModel.
That is, the class signature of the beginning looked like this:
class MainWindowViewModel
{
}
after correction like this:
class MainWindowViewModel : INotifyPropertyChanged
{
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question