Posts

Using the ComboBox SelectedItem property in WPF / MVVM

Image
An implementation of using the WPF Combo Box using MVVM patterns. The intention is to get a ComboBox control's SelectedItem property to bind to an instance of a selected object, so that when the SelectedItem is changed, all other entities that are bound to it are also updated. See this StackOverflow discussion for background information: http://stackoverflow.com/questions/11062297/wpf-mvvm-combobox-selecteditem Step 1: Create a new WPF project: Step 2: Create your classes In my example I use a 'Languages' class that holds a language value, such as "English" and another string to identify it, such as "en": Languages.cs [code language="csharp"] using System.ComponentModel; using System.Runtime.CompilerServices; using WpfComboBox.Annotations; namespace WpfComboBox { public class Language : INotifyPropertyChanged { private string _languageValue; private string _languageId; public string Lan...

Controlling DocumentViewer methods and properties using MVVM

Image
In this post I demonstrate how the methods of a DocumentViewer class may be invoked via MVVM / WPF in order to modify the way an embedded PowerPoint presentation is displayed. Step 1: Create a new WPF application Step 2: Create the Main Window view Just a simple view to house the DocumentViewer control and a button with which to tell it to launch the PowerPoint presentation. We will fill in the various command binding a bit later. [code language="xml"] <Window x:Class="DocumentView.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:DocumentView" mc:Ignorable="d" WindowState="{Binding Windo...

How to bind the WindowState property of a window in WPF using MVVM

Image
Step 1: Create a new WPF project Step 2: Create the ViewModel classes Add the ViewModel classes that are used to do the bindings with the User Interface components MainWindowViewModel.cs This includes the WindowState property and setting this state to maximized upon construction: [code language="csharp"] namespace WindowState { public class MainWindowViewModel : ViewModelBase { private System.Windows.WindowState _windowState; public MainWindowViewModel() { WindowState = System.Windows.WindowState.Maximized; } public System.Windows.WindowState WindowState { get { return _windowState; } set { _windowState = value; OnPropertyChanged("WindowState"); } } } } [/code] ViewModelBase.cs [code language="csharp"] using System.ComponentModel; namespace WindowState { public class Vi...

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

Image
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 WPF implementation created in Visual Studio 2015. Step 1: Create a WPF application Step 2: Add the System.Windows.Interactivity reference UPDATE For more recent versions of .NET see this StackOverflow post on how to upgrade from older versions of Interactivity to the latest NuGet package: https://stackoverflow.com/questions/8360209/how-to-add-system-windows-interactivity-to-project/56240223#56240223 If you need to migrate from an older version, then do these steps: 1. Remove reference to “Microsoft.Expression.Interactions” and “System.Windows.Interactivity” 2. Install the Microsof...

Embedding PowerPoint documents in WPF

Image
Some instruction on how to embed a PowerPoint presentation within a WPF application by converting the file to a XPS format and displaying this using DocumentViewer. Step 1: Create a WPF Application Step 2. Include the necessary references Microsoft.Office.Interop.PowerPoint Office ReachFramework Step 3: Insert DocumentViewer control into MainWindow.xaml [code language="xml"] <Window x:Class="DocumentViewer.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" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <DocumentViewer Name="Documentv...

Using the Mediator pattern in MVVM / WPF

Image
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 child views, View1 and View2. Step 1: Create a new WPF application Step 2: Add event handling classes EventArgs.cs [code language="csharp"] using System; namespace MvvmSwitchViews { public class EventArgs<T> : EventArgs { public EventArgs(T value) { Value = value; } public T Value { get; private set; } } } [/code] EventRaiser.cs [code language="csharp"] using System; namespace MvvmSwitchViews { public static class EventRaiser { public static void Raise(this EventHandler handler, object sender) { if (handler != null) { ...

Using the CefSharp Chromium Web Browser in WPF / XAML

Image
Some brief instructions on getting started with using the Chromium Web Browser in WPF. Step 1: Create a new WPF project Step 2: Obtain the CefSharp packages using NuGet Link for the NuGet packages is here: https://www.nuget.org/packages/CefSharp.WPF/ Select Tool > NuGet Package manager > Package Manager Console. Use the [code language="txt"] PM> install-package CefSharp.Wpf [/code] Step 3: add the CefSharp dll references Right-click on References, select 'Add reference' When the dialog appears, select the Browse button. Navigate to the 'packages' folder that NuGet has installed to your Visual Studio project. For this project I'm choosing the x86 versions... Add CefSharp.Wpf.dll: Add the CefSharp.dll, CefSharp.Core.dll, CefSharp.BrowserSubprocessCore.dll: If you get the error similar to the following: [code language="text"] Error 1 CefSharp.Common will work out of the box if you specify platform (x86 / ...