Posts

Showing posts with the label XAML

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

Using RelayCommand / ICommand to handle events in WPF and MVVM

Image
A step-by-step guide to using the RelayCommand class (based on ICommand) as means of handling user-initiated events in WPF / MVVM / XAML. 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: The MainWindowViewModel class will be used implement a very simple function to display a message box when it receives the incoming event. Add the functions and code to MainWindowViewModel.cs as follows: [code language="csharp"] using System.Windows; using System.Windows.Input; namespace Command { public class MainWindowViewModel { private ICommand _command; public ICommand Command { get { return _command ?? (_command = new RelayCommand( x => { DoStuff(); })); } } private static void DoStuff() { ...

Switching between WPF XAML views using MVVM DataTriggers

Image
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 windows can be made to switch between different views, create two new XAML views, View1.xaml and View2.xaml, each consisting of a simple TextBlock containing different texts to demonstrate that the views are different. In your Visual Studio project, right-click your project folder and select Add > New Item. Select User Control (WPF) and name your file as View1.xaml: In the xaml portion of the file, insert the code to display the Text in the view as follows: [code language="xml"] <UserControl x:Class="XamlSwitching.View1" xmlns=...

WPF Binding an Image using XAML and MVVM

Image
A post describing how to bind an image such as a bitmap or png file to your viewmodel. In Visual Studio create a new WPF project: