Posts

Showing posts from January, 2017

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...