Используется паттерн MVVM. База данных обновляется при добавлении строки. Коллекция также содержит измененные или добавленные строки, но DataGrid не обновляется.
Model
namespace Terminal_Forts_ММВБ.Model
{
public class PortfelStrateg : ViewModelINPC
{
private int id;
public int Id
{
get => id; set
{
if (id != value)
{
_ = SetField(ref id, value);
}
}
}
private string portfel;
public string Portfel
{
get => portfel; set
{
if (portfel != value)
{
_ = SetField(ref portfel, value);
}
}
}
private string instrument;
public string Instrument
{
get => instrument; set
{
if (instrument != value)
{
_ = SetField(ref instrument, value);
}
}
}
private double zena_tek;
public double Zena_tek
{
get => zena_tek; set
{
if (zena_tek != value)
{
_ = SetField(ref zena_tek, value);
}
}
}
}
}
public class ViewModelINPC : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = "")
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
}
namespace Terminal_Forts_ММВБ.Model
{
public static class DataWorker
{
public static ObservableCollection<PortfelStrateg> GetAllPortfelStrategs()
{
using (ApplicationContext db = new ApplicationContext())
{
ObservableCollection<PortfelStrateg> portfelStrategsObservable = new ObservableCollection<PortfelStrateg>();
var result = db.PortfelStrategs.ToList();
for (int i = 0; i < result.Count; i++)
{
portfelStrategsObservable.Add(result[i]);
}
return portfelStrategsObservable;
}
}
}
}
ViewModel
namespace Terminal_Forts_ММВБ.ViewModel
{
public class ApplicationViewModel : ViewModelINPC
{
#region Вывести весь список (binding на таблицы)
//Таблица PortfelStrateg
private ObservableCollection<PortfelStrateg> allPortfelStrategs = DataWorker.GetAllPortfelStrategs();
public ObservableCollection<PortfelStrateg> AllPortfelStrategs
{
get => allPortfelStrategs;
set
{
if (allPortfelStrategs != value)
{
_ = SetField(ref allPortfelStrategs, value);
}
}
}
#endregion
#region Метод редактирования dataGridPortfelStrateg по событию колбэка
public void EditDataGridPortfelStrategMetodEvent()
{
//Вычисления и запись в базу данных
AllPortfelStrategs = DataWorker.GetAllPortfelStrategs();
}
}
}
View
namespace Terminal_Forts_ММВБ.View
{
public partial class MainWindow : Window
{
private readonly ApplicationViewModel viewModel = new ApplicationViewModel();
public MainWindow()
{
InitializeComponent();
DataContext = viewModel;
}
}
}
Xaml
<Window
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:viewmodel="clr-namespace:Terminal_Forts_ММВБ.ViewModel"
xmlns:wpf="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
x:Class="Terminal_Forts_ММВБ.View.MainWindow"
mc:Ignorable="d"
<Window.DataContext>
<viewmodel:ApplicationViewModel/>
</Window.DataContext>
<Grid>
<DataGrid ItemsSource="{Binding AllPortfelStrategs}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Portfel}"/>
<DataGridTextColumn Binding="{Binding Instrument}"/>
<DataGridTextColumn Binding ="{Binding Zena_tek}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
AllPortfelStrategs
?.Add()
или любой другой метод коллекции - это идет изменение коллекции, за это отвечаетINotifyCollectionChanged
, который по умолчанию есть вObservableCollection
и вBindingList
, всем остальным коллекциям нужно реализовывать этот интерфейс для обновления UI.