Posts

Showing posts with the label WPF

How to wrap text around another control in WPF

Image
First things first, credit where it's due: the StackOverflow link I eventually found that enabled me to solve this problem: https://stackoverflow.com/questions/3339051/wrapping-text-around-an-image-or-linking-two-textblocks-in-c-sharp-wpf The trick is not use use a TextBlock as conventional wisdom would indicate, but to use a FlowDocument to achieve this kind of layout. For future reference (I am going to need something like this fairly soon), try the following XAML snippet and the resulting WPF app screenshot to demonstrate how to display a button in the top right hand corner with text wrapped around it. In my real-world apps I am using the MVVM pattern with all sorts of bindings and events etc, but this should hopefully enable me to get there: MainWindow.xaml [code language="xml"] <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.mi...

Binding the visibility of DataGridColumn in WPF / MVVM

Image
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 how to achieve this in a Visual Studio WPF application. Step 1: Create a Visual Studio WPF application: Step 2: Create a Freezable class "Freezable objects can inherit the DataContext even when they’re not in the visual or logical tree. So, we can take advantage of that to our use. First create class inheriting from Freezable and Data DP which we can use to bind in XAML". BindingProxy.cs [code language="csharp"] using System.Windows; namespace DataGridColumnVisibility { public class BindingProxy : Freezable { public static readonly DependencyProperty DataProperty = ...

Using Expander controls in C# / WPF

Image
A short post demonstrating how to use expander controls in WPF, to create a list of collapsible / expandable menu items in your C# WPF application Create a new Visual Studio application Create a View Model class for your main window MainWindowViewModel.cs [code language="csharp"] using System.Collections.Generic; namespace ExpanderExample { public class ApplicationListItem { public ApplicationListItem(string name, string openButtonText, string updateButtonText, bool updateButtonVisibility = false, bool progressVisibility = true) { Name = name; OpenButtonText = openButtonText; UpdateButtonText = updateButtonText; UpdateButtonVisibility = updateButtonVisibility; ProgressVisibility = progressVisibility; } public string Name { get; set; } public string OpenButtonText { get; set; } public string UpdateButtonText { get; set; } public bool UpdateButtonVisibility {...

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

Image
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 of the two opt algorithm. http://en.wikipedia.org/wiki/2-opt But essentially the 2-opt link swapping heuristic can be summarised by the following steps: The actual 2-opt heuristic can be summarised by the following pseudocode steps, repeating for all feasible combinations of I and k: [code language="text"] 1. take route[1] to route[i-1] and add them in order to new_route 2. take route[i] to route[k] and add them in reverse order to new_route 3. take r...

Using HierarchicalDataTemplate with TreeView in WPF / MVVM

Image
Some instructions on how to create and display a hierarchical data structure in WPF by using HierarchicalDataTemplate with TreeView Visual Studio 2019 project downloadable from here. Stack Overflow link Step 1: Create a new WPF application Step 2: Create the model class The Model class we use to define our Directory 'Item' structure. In this instance I'm keeping things really simple, just a 'Name' and a List of other 'DirectoryItem' objects. Also contained in this class is a recursive function to traverse the 'DirectoryItem' node elements: Model.cs [code language="csharp"] using System.Collections.Generic; namespace WpfTreeViewBinding.Model { public class Item { public string Name { get; set; } } public class DirectoryItem : Item { public DirectoryItem() { Items = new List<DirectoryItem>(); } public List<DirectoryItem> Items { get; set; } ...

How to use resource files in your C# WPF project

Image
Some practical instructions on how to include resource files to your Visual Studio C# WPF project that are not source code. Such non-code files can include binary data, text files, media files, string values, icons, XML, or any other type of data. Project resource data is stored in XML format in the .resx file (named Resources.resx by default). In this example we demonstrate how resource files can be defined in and accessed from a separate class library, thereby helping to make your project cleaner and less cluttered. Step 1: Create a new Visual Studio WPF project. Create a new WPF application for this example: Step 2: Add a new class library project Right-click on your solution folder and select Add > New Project. Create a new Class Library application. We'll call it FileStore: This project we will use to contain our resource files. Step 3: Create a folder to store the resource files In the 'FileStore' Class Library project you created, right...

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

Using embedded MediaElement controls to play videos in WPF / MVVM

Image
Essentially the same as these instructions , but using a purely MVVM approach and without the additional stop / start / pause etc buttons - the application just 'sees' the MP4 media file it needs to play and plays it upon startup. Step 1: Create a new WPF Application Step 2: Create the Main Window ViewModel classes Add the following classes to your project: MainWindowViewModel.cs [code language="csharp"] namespace MediaElement { public class MainWindowViewModel : BaseViewModel { private string _url; public MainWindowViewModel() { Url = "E:\\temp\\SampleMp4File.mp4"; } public string Url { get { return _url; } set { if (_url == value) return; _url = value; OnPropertyChanged("Url"); } } } } [/code] BaseViewModel.cs [code language="csharp"] usin...