Допустим есть три файла, MainWindowViewModel, TCommand и window.xaml, сама среда разработки видит всё вплоть до типов переменных у полей объекта User (id - string, name - string, age - string, balance - int), но при нажатии на кнопку ShowAll ничего не происходит :
--- 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>