Posts

Showing posts with the label MVVM

Donate

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

Image
Good day to all! Here's an example of a WPF CRUD (Create,Update and Delete) project using the DataGrid control, ADO.NET Entity Framework 6.x, C#.NET and Model–View–Viewmodel(MVVM) architectural pattern. This post was based from this tutorial WPF CRUD With DataGrid, Entity Framework And C#.NET except that we are now using the MVVM framework. The steps below are pretty straightforward and easy to follow. I. Project Setup 1. Add a table called Students in you database. The complete script is found in this post WPF CRUD With DataGrid, Entity Framework And C#.NET 2. Create a WPF Project and add four folders called DataAccess, Model, View and ViewModel. 3. Your project structure may look similar with the screenshot provided below. II. Coding The Model and Repository Class 1. Inside the Model folder, add an ADO.NET Entity Data Model that connects to the Students table in your database. On my part, I named it StudentModel . 2. For the connectionstring name in App.config file, I

WPF MVVM - No connection string named 'MyEntities' could be found in the application config file.

Image
Hello Team, While working on a simple WPF MVVM application using Entity Framework 6.x and Visual Studio 2019, I encountered this strange error No connection string named 'MyEntities' could be found in the application config file . However, if I compile and debug my application, it would proceed with running the project. I've searched lots of information and most of the solutions presented were to copy the web.config or app.config file from a class library to the project that is set as the startup application. One thing I've noticed though is that this error appears only on the XAML page of where the DataContext is assigned. After doing some experiments of modifying my application, I came up with a solution that is instead of setting the DataContext in the XAML like below: <Window.DataContext> <viewModel:StudentViewModel/> </Window.DataContext> I assigned the DataContext in the code behind of the MainWindow and thus the error message disappeared.

Passing Data From One Window To Another in WPF

Good afternoon everyone! When passing data from one window to another in WPF, the ideal approach is to set the both window's DataContext properties with the same ViewModel. In such a way, if a property in a View Model is updated in window1, window2 can access that property and the new value that has reflected. The snippet below show a child window and set's it's DataContext property using the main window's DataContext property. So whatever changes happen to a view model's property value, main window can also get a copy of that new value. public void ShowWindow(Window childWindow, bool isDialog) { //Application.Current.Windows[0] is the main window //set DataContext of child window with DataContext of main window var vm = Application.Current.Windows[0].DataContext; if (childWindow != null ) { childWindow.DataContext = vm; if (isDialog) childWindow.ShowDialog(); else childWindow.Show(); } }

WPF Busy Overlay Or Busy Indicator Example In C#

Image
Greetings all! I've been doing some research on how to add an overlay feature similar to an Ajax busy modal to one of my WPF projects. A simple approach led me to this topic WPF: Simple "Busy" Overlay . Since I'm using the traditional MVVM and not Simon Cropp’s Notify Property Weaver, I made this work by creating a WPF project using Visual Studio 2017 (installed in my workstation) and then copied the codes specifically BoolToVisibilityConverter.cs , BusyViewModel.cs , DelegateCommand.cs and MainWindow.xaml from his example at BitBucket . I also changed the IsBusy property to implement the OnPropertyChanged event private bool _IsBusy; public bool IsBusy { get { return _IsBusy; } set { _IsBusy = value ; OnPropertyChanged( "IsBusy" ); } } and added the implementation of that event. protected void OnPropertyChanged ( string propertyName) { if (PropertyChanged != null ) PropertyChanged( this , new PropertyChangedEventArgs(propertyName)

MVVM RelayCommand And ViewModelBase Classes in VB.NET

Good evening! Here's a conversion of the RelayCommand and VewModelBase classes to VB.NET as reference for VB.NET programmers. RelayCommand Imports System.Windows.Input Public Class RelayCommand : Implements ICommand Private ReadOnly _execute As Action( Of Object ) Private ReadOnly _canExecute As Predicate( Of Object ) Public Sub New ( ByVal execute As Action( Of Object )) End Sub Public Sub New ( ByVal execute As Action( Of Object ), ByVal canExecute As Predicate( Of Object )) If execute Is Nothing Then Throw New ArgumentNullException( "execute" ) _execute = execute _canExecute = canExecute End Sub Public Function CanExecute ( ByVal parameter As Object ) As Boolean Implements ICommand.CanExecute Return (_canExecute Is Nothing ) OrElse _canExecute(parameter) End Function Public Custom Event CanExecuteChanged As EventHandler Implements ICommand

Refactor An MVVM ViewModel Class

Good afternoon! I recently asked the forums on how to refactor a ViewModel class given that in the future additional features or functionalities will be added, so the ViewModel class becomes bloated and hard to trace. All the commands, database operations and properties are declared in this class. So, the option I was thinking was segregation but I also need inputs from other experienced developers in the community. Sample ViewModel Class namespace MVVM.MainApplication.ViewModel { public class EmployeeViewModel : ViewModelBase { private ICommand _saveCommand; private ICommand _resetCommand; private ICommand _editCommand; private ICommand _deleteCommand; private Employee _employeeEntity; private EmployeeRepository _repository; private EmployeeModel _employeeModel; public EmployeeModel EmployeeModel { get { return _employeeModel; }

MVVM Basics With TextBlock Control

Hello, This post is based on the article Understanding the basics of MVVM design pattern . The author demonstrated the basics of MVVM using TextBlock controls. However, the code samples have several issues and in order for the sample application to work, I revise them with the following changes. BindableBase.cs - since SetProperty method uses T in it's parameter, you also need to reference T in your classname. public class BindableBase <T> : INotifyPropertyChanged { ... } MainPageViewModel.cs - update the code in the constructor to bind a single object to the TextBlock controls. public class MainPageViewModel : BindableBase<Book> { private Book _book; public Book Book { get { return _book; } set { SetProperty( ref _book, value ); } } public MainPageViewModel() { Book = new Book() { Title = "Harry Potter" , Author = "J. K. Rowling" , Category = "Young-adult fiction"

WPF DataGrid Data Binding Using MVVM Pattern

Image
Hello, I've been developing WPF applications before but not having applied the MVVM design pattern and have been wanting to create a simple program that loads data into the DataGrid control. As a result of free time, here's a simple demonstration on using MVVM Pattern for Data Binding a WPF DataGrid control. According to Wikipedia, the components of an MVVM pattern are: Model Model refers either to a domain model, which represents real state content (an object-oriented approach), or to the data access layer, which represents content (a data-centric approach). View As in the MVC and MVP patterns, the view is the structure, layout, and appearance of what a user sees on the screen. View model The view model is an abstraction of the view exposing public properties and commands. Instead of the controller of the MVC pattern, or the presenter of the MVP pattern, MVVM has a binder. In the view model, the binder mediates communication between the view and the data binder.The view

Donate