I delve into WPF and MVVM. From the beginning I wrote this:
private void Button_Click(object sender, RoutedEventArgs e) { if(String.IsNullOrEmpty(txtAccountLogin.Text) || String.IsNullOrEmpty(txtAccountPassword.Text)) { MessageBox.Show("Все поля должны быть заполнены!", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error); } else { using (Cursor = Cursors.Wait) { using (AccountContext ac = new AccountContext()) { Account account = new Account(); account.login = txtAccountLogin.Text; account.password = txtAccountPassword.Text; ac.Accounts.Add(account); ac.SaveChanges(); MessageBox.Show("Аккаунт " + account.login + " добавлен.", "Сообщение", MessageBoxButton.OK, MessageBoxImage.Information); txtAccountLogin.Text = ""; txtAccountPassword.Text = ""; } } } } But then I realized that the code should not know anything about the interface. The question is, I need to create a "account" model, inherit it from INotifyPropertyChanged to make event changes fields. Then you need to make an AccountViewModel and in it and beyond I do not understand what's next. By clicking the button, what should I contact to get the text from the controls? Do I understand correctly that because of the change event, the binding will also change the state of the property? Here is my example of how I did it with the DataGrid :
class Message: INotifyPropertyChanged { public string author { get; set; } public string message { get; set; } public string bulletinText { get; set; } public int id { get; set; } public event PropertyChangedEventHandler PropertyChanged; } class MessageViewModel: INotifyPropertyChanged { private Message Message { get; set; } public ObservableCollection<Message> Messages { get; set; } public MessageViewModel() { Messages = new ObservableCollection<Message> { new Message{ author = "Федор Иванович", bulletinText = "Привет, как дела?", message = "Продам собаку", id = 154 }, new Message{ author = "Меткий снайпер", bulletinText = "Сообщите номер накладной?", message = "Доставка овощей", id = 255} }; } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged([CallerMemberName]string prop = "") { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(prop)); } } public Main() { InitializeComponent(); DataContext = new MessageViewModel(); } XAML:
<DataGrid CanUserAddRows="False" ItemsSource="{Binding Messages}" DockPanel.Dock="Top" AutoGenerateColumns="False" HorizontalAlignment="Stretch" VerticalScrollBarVisibility="Auto" VerticalAlignment="Stretch" Margin="5,0,0,0" MouseDoubleClick="DataGrid_MouseDoubleClick"> <DataGrid.Columns> <DataGridTextColumn Header="От кого" Binding="{Binding Path=author}" Width="*" IsReadOnly="True" /> <DataGridTextColumn Header="Сообщение" Binding="{Binding Path=message}" Width="*" IsReadOnly="True" /> <DataGridTextColumn Header="Превью объявления" Binding="{Binding Path=bulletinText}" Width="*" IsReadOnly="True" /> </DataGrid.Columns> </DataGrid> This is how I capture the click event:
private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e) { DataGrid grid = (DataGrid)sender; int id = (grid.SelectedItem as Message).id; MessageBox.Show(id.ToString()); } How to do in the first part of my question?
INotifyPropertyChangedonly the view model. This interface is used to notify UI about changes in view models - tym32167public void OnPropertyChanged([CallerMemberName]string prop = "")- then why do you need this method? From the fact that you have implemented the interface, nothing will change if you do not use interface methods. If you do not need it - delete - tym32167if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(prop));you can simply replace withPropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));- tym32167Но потом понял что код не должен знать ничего о интерфейсе.- I'll tell you more, in the full MVVM events are not relevant (Click,MouseDoubleClick, etc.), instead of them they are used to bind to commands. Just imagine that you don’t have an interface at all, you are writing an application, and another person sitting in another office is developing an interface without your code and only one DataContext has to link them - this is the correct MVVM. AlsoMessageBox- they are also not so easy to implement, usually an interface is created for such purposes. Well, in general, I do not quite understand your question, what does the TextBox have to do with it? - EvgeniyZ pm