Posts

Showing posts with the label WPF

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.

WPF CRUD With DataGrid, RepoDB ORM And C#.NET

Image
Good day Gents, Here's a basic tutorial on how to integrate the RepoDB ORM into your WPF desktop application. I already have written an article in ASP.NET MVC web application here using the said ORM and I realized that I have to create an example for the desktop as well. To proceed working with the code, you need to run the create table script from this post WPF CRUD With DataGrid, Entity Framework And C#.NET . Next is to add a model class that maps the information from the database table to an object or list of objects. public class Students { public int ID { get ; set ; } public string Name { get ; set ; } public int Age { get ; set ; } public string Address { get ; set ; } public string Contact { get ; set ; } public Students() { ID = 0; Name = string .Empty; Age = 0; Address = string .Empty; Contact = string .Empty; } } Add an interface to the project that has the method signatures used in the actual repository class. The interface meth

XmlDataProvider With TwoWay Binding In WPF

Hello, Here's a simple example of two-way binding in WPF using XMLDataProvider. This post is taken from John Papa's example in MSDN magazine years ago but with minor errors. The XMLDataProvider markup has color values and is declared inside Window.Resources node. <Window.Resources> <XmlDataProvider x:Key= "MoreColors" > <x:XData> <colors xmlns= '' > <color name= "pink" /> <color name= "white" /> <color name= "black" /> <color name= "cyan" /> <color name= "gray" /> <color name= "magenta" /> </colors> </x:XData> </XmlDataProvider> </Window.Resources> The Textbox control's Text Text property binding has been set to TwoWay so when you enter a color name it will be added as a ListBox Item. <Grid> <StackPanel> <TextBlock Width= "248" Height= "24"

Typography.Fraction and Typography.Capitals Not Working In WPF

I've been reading a post on working with Typography.Fraction and Typography.Capitals Enum on TextBlock controls and cannot get it to work as expected. After reading the Microsoft Docs, the solution was to set the FontFamily of the control to Palatino Linotype. <TextBlock Grid.Row= "0" FontSize= "32" FontFamily= "Palatino Linotype" Typography.DiscretionaryLigatures= "True" Typography.ContextualLigatures= "True" Typography.StandardLigatures= "True" > Quite effective find </TextBlock> <TextBlock Grid.Row= "1" FontSize= "32" FontFamily= "Palatino Linotype" Typography.Capitals= "AllSmallCaps" > Hello, World </TextBlock> <TextBlock Grid.Row= "2" FontSize= "32"

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)

How To Reference A Converter Class In A Resource Dictionary In WPF

Good evening all! Given that you have a Resource Dictionary file that has a Style property that will use a Converter class for Binding, the steps to reference that class are as follows. Assuming that you have a converter class such as an EmployeeTargetConverter: namespace EmployeeManagement { public class EmployeeTargetConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { //converter codes here..... } public object ConvertBack ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException (); } } } To reference that class, you must include the namespace of that project in the ResourceDictionary element. <ResourceDictionary xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml"

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; }

Donate