Информации по данной теме мало, собирал ее по крупицам. Я хочу, чтобы при вводе в текстбоксы изменялись поля объекта NewNote (чтобы потом добавить этот объект в базу данных, этот код я закомментировал). При этом команды, определенные во ViewModel, работают исправно.
Модель:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace MVVM
{
class Note : INotifyPropertyChanged
{
private string title;
private string text;
public int Id { get; set; }
public string Title
{
get { return title; }
set
{
title = value;
OnPropertyChanged("Title");
}
}
public string Text
{
get { return text; }
set
{
text = value;
OnPropertyChanged("Text");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName]string prop = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
}
ViewModel:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Entity;
using System.Data.SQLite;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace MVVM
{
class ViewModel : INotifyPropertyChanged
{
/* ApplicationContext DB;
IEnumerable<Note> notes;*/
RelayCommand addNote;
Note newNote = new Note();
public Note NewNote
{
get { return newNote; }
set
{
newNote = value;
OnPropertyChanged("NewNote");
}
}
public RelayCommand AddNote
{
get
{
return addNote ??
(addNote = new RelayCommand((o) =>
{
MessageBox.Show(NewNote.Title);
}));
}
}
/*public IEnumerable<Note> Notes
{
get { return notes; }
set
{
notes = value;
OnPropertyChanged("Notes");
}
}
public ViewModel()
{
DB = new ApplicationContext();
DB.Notes.Load();
Notes = DB.Notes.Local.ToBindingList();
}*/
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName]string prop = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
}
Veiw:
<Window x:Class="MVVM.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:MVVM"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="0.85*"/>
</Grid.ColumnDefinitions>
<TextBox Margin="25, 40, 50, 50" MinHeight="50" Text="{Binding ElementName=NewNote,Path=Title,UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Grid.Row="1" Margin="25, 0, 50, 60" TextWrapping="Wrap" Text="{Binding ElementName=NewNote,Path=Text,UpdateSourceTrigger=PropertyChanged}"/>
<Button Grid.Row="2" Margin="25, 0, 50, 30" MinHeight="50" Content="Add note" Command="{Binding AddNote}"/>
<ListBox Grid.Column="1" Grid.Row="0" Grid.RowSpan="3"/>
</Grid>
</Window>
Класс RelayModel сюда вставлять не стал