Dragging shapes with the mouse in WPF / MVVM

In a recent project I was interested in Dragging shapes with the mouse in a WPF / MVVM Visual Studio project. This post contains an example on how to drag a rectangle from from one canvas location to another using the WPF / MVVM architecture. This project uses Microsoft Visual Studio Community 2019. Step 1: …

Continue reading ‘Dragging shapes with the mouse in WPF / MVVM’ »

Adding tab items dynamically in WPF / MVVM

Some instructions on how to add/remove tab items within a WPF / MVVM setting. Step 1: Create a new WPF application Step 2: Add classes to implement ICommand RelayCommand.cs EventRaiser.cs EventArgs.cs Step 3: Add the ViewModel class We need a class to implement the button click events, as well contain data for tab-related information. MainWindowViewModel.cs …

Continue reading ‘Adding tab items dynamically in WPF / MVVM’ »

Binding the visibility of DataGridColumn in WPF / MVVM

See this StackOverflow answer for the original inspiration. Notable quote: “First of all DataGridTextColumn or any other supported dataGrid columns doesn’t lie in Visual tree of DataGrid. Hence, by default it doesn’t inherit DataContext of DataGrid. But, it works for Binding DP only and for not other DP’s on DataGridColumn.” Here is an example of …

Continue reading ‘Binding the visibility of DataGridColumn in WPF / MVVM’ »

Applying the 2-opt algorithm to travelling salesman problems in C# / WPF

For the Java equivalent see this link: https://www.technical-recipes.com/2017/applying-the-2-opt-algorithm-to-traveling-salesman-problems-in-java/ For the C++ equivalent see this link: https://www.technical-recipes.com/2012/applying-c-implementations-of-2-opt-to-travelling-salesman-problems/ This post demonstrates how to apply the 2-opt algorithm to a number of standard test problems in C# while displaying the results in a WPF style window, while using the MVVM design pattern. See this link for an overview …

Continue reading ‘Applying the 2-opt algorithm to travelling salesman problems in C# / WPF’ »

Using the Supervisor Controller Pattern to access View controls in MVVM

As discussed on a StackOverflow post, this is considered bad practice, so only use this when necessary. Step 1: Create a new WPF project Step 2: Add event handling code A number of classes are needed for doing command bindings in MVVM. Create the following classes and add to your Visual Studio project: RelayCommand.cs EventRaiser.cs …

Continue reading ‘Using the Supervisor Controller Pattern to access View controls in MVVM’ »

How to use Interaction Triggers to handle user-initiated events in WPF / MVVM

Example scenario: User clicks the mouse in a WPF application – how do we ‘listen’ to that event in order to trigger and handle an event in the main code? A possible solution is to use in WPF MVVM. See this post to learn how to do this using MvvmLight: https://www.technical-recipes.com/2017/handling-mouse-events-in-wpf-mvvm-using-mvvmlight-event-triggers/ Here is an example …

Continue reading ‘How to use Interaction Triggers to handle user-initiated events in WPF / MVVM’ »

Using the Mediator pattern in MVVM / WPF

Some instructions on how to use the Mediator design pattern as a means of allowing communication between ViewModel classes in your MVVM / WPF application. In this example I use the Mediator as a means of communicating to the main window which view to display when the user clicks a button on either of the …

Continue reading ‘Using the Mediator pattern in MVVM / WPF’ »

Binding data to ListView controls in WPF using MVVM

Some few tips and instructions on how to bind data items contained in a List to a WPF ListView. Create a new empty WPF Application: Then create a new ViewModel class for out MainWindow.xaml which will be used to create and access the data items that are bound to the ListView: Our MainWindowViewModel class has …

Continue reading ‘Binding data to ListView controls in WPF using MVVM’ »

Binding a XAML control visibility to a ViewModel variable in WPF

Some instructions on how to bind the visibility of a XAML button control to a boolean value in a ViewModel class. 1. Create a new WPF application 2. Add the View Model class Right click your project folder and select Add > New Item > Class. Name your class MainWindowViewModel.cs: In your ViewModel class include …

Continue reading ‘Binding a XAML control visibility to a ViewModel variable in WPF’ »

Switching between WPF XAML views using MVVM DataTriggers

Related post: https://www.technical-recipes.com/2016/switching-between-wpf-xaml-views-using-mvvm-datatemplate/ This post addresses the problem of being able to switch between different views based on the property of a ViewModel class. To get started in Visual Studio, create a new WPF Application: So that when we build and run the application we have a blank window like this: To demonstrate how WPF …

Continue reading ‘Switching between WPF XAML views using MVVM DataTriggers’ »