Saturday, November 30, 2019

.NET Core Book Review on Morning Dew

Hello WPF developers!

I just published a book review on my Morning Dew blog that may be of interest to my readers here. C# 8.0 and .NET Core 3.0 - Modern Cross-Platform Development is a new title by Mark Price from Packt Publishing. While it covers a broad range of .NET Core topics, there are chapters on both Xamarin and Windows Desktop application development.

Go check it out here!

Friday, December 21, 2018

WPF Tip #21 - Create a Reusable ToolTip With a Header and Icon

In this tip, we're going to create a custom control derived from ToolTip to create a headered tooltip with an icon. Creating it as a custom control makes it trivial to add the tooltip to any control in your project.

This simple example will include a specific icon and has a property to hide/show the icon. I'll explain later how you could update the control's implementation to accept an icon through binding, enabling different icons across different uses.

The first step is to create the custom control class that inherits from ToolTip. Here is the implementation for HeaderedToolTip.

[TemplatePart(Name = PART_TooltipHeader, Type = typeof(TextBlock))]
[TemplatePart(Name = PART_TooltipIcon, Type = typeof(ContentControl))]
[TemplatePart(Name = PART_TooltipContents, Type = typeof(TextBlock))]
public class HeaderedToolTip : ToolTip
     private const string PART_TooltipHeader = "PART_TooltipHeader";
     private const string PART_TooltipIcon = "PART_TooltipIcon";
     private const string PART_TooltipContents = "PART_TooltipContents";
     private TextBlock _headerBlock = null;
     private ContentControl _iconControl = null;
     private TextBlock _contentsBlock = null;
     static HeaderedToolTip()
         DefaultStyleKeyProperty.OverrideMetadata(typeof(HeaderedToolTip), new FrameworkPropertyMetadata(typeof(HeaderedToolTip)));
     public string HeaderText
         get => (string)GetValue(HeaderTextProperty);
         set => SetValue(HeaderTextProperty, value);
     // Using a DependencyProperty as the backing store for HeaderText. This enables animation, styling, binding, etc.
     public static readonly DependencyProperty HeaderTextProperty =
         DependencyProperty.Register("HeaderText", typeof(string), typeof(HeaderedToolTip), new UIPropertyMetadata(string.Empty));
     public string ContentText
         get => (string)GetValue(ContentTextProperty);
         set => SetValue(ContentTextProperty, value);
     // Using a DependencyProperty as the backing store for HeaderText. This enables animation, styling, binding, etc.
     public static readonly DependencyProperty ContentTextProperty =
         DependencyProperty.Register("ContentText", typeof(string), typeof(HeaderedToolTip), new UIPropertyMetadata(string.Empty));
     public Visibility IconVisibility
         get => (Visibility)GetValue(IconVisibilityProperty);
         set => SetValue(IconVisibilityProperty, value);
     // Using a DependencyProperty as the backing store for IconVisibility. This enables animation, styling, binding, etc.
     public static readonly DependencyProperty IconVisibilityProperty =
         DependencyProperty.Register("IconVisibility", typeof(Visibility), typeof(HeaderedToolTip), new UIPropertyMetadata(Visibility.Collapsed));
     public override void OnApplyTemplate()
         _headerBlock = GetTemplateChild(PART_TooltipHeader) as TextBlock;
         _iconControl = GetTemplateChild(PART_TooltipIcon) as ContentControl;
         _contentsBlock = GetTemplateChild(PART_TooltipContents) as TextBlock;

Some of this code is not necessary for our tooltip, but I added it here to illustrate how you can get a reference to parts of your custom control within the class in OnApplyTemplate(). This would be useful if you need to respond to any events fired from the controls that make up your custom controls. You would add the event handler hooks here after resolving them through GetTemplateChild.

The next step is to set up the template in a ResourceDictionary. Here is the XAML markup from my ToolTipResourceDictionary.xaml file.

<ResourceDictionary xmlns=""
         <ResourceDictionary Source="..\Images\vector_Information.xaml"/>
     <Style TargetType="{x:Type local:HeaderedToolTip}">
         <Setter Property="Template">
                 <ControlTemplate TargetType="{x:Type local:HeaderedToolTip}">
                     <Border BorderThickness="2" BorderBrush="Black">
                         <Grid Background="Ivory">
                                 <RowDefinition Height="Auto"/>
                                 <RowDefinition Height="*"/>
                                 <ColumnDefinition Width="*"/>
                                 <ColumnDefinition Width="Auto"/>
                             <TextBlock Text="{TemplateBinding HeaderText}"
                             <ContentControl Grid.Column="1"
                                         Content="{StaticResource vector_Information}"
                                         Visibility="{TemplateBinding IconVisibility}"
                                         Height="16" Width="16"
                             <TextBlock Grid.Row="1"
                                    Text="{TemplateBinding ContentText}"

The final step is to consume our custom tooltip in a control somewhere in our project. I have create a Window containing a horizontally aligned StackPanel with two Grids. Each grid contains a border that glows a different color when you mouse over it, one blue, one gold. The second grid is disabled to demonstrate the ToolTipService.ShowOnDisabled attached property. Each Grid.ToolTip contains an instance of our HeaderedToolTip custom tooltip control.

         <ResourceDictionary Source="..\ToolTipResourceDictionary.xaml"/>
     <StackPanel Orientation="Horizontal">
         <Grid ToolTipService.ShowOnDisabled="True">
             <Border Width="180" Height="180"
                 Margin="10" Background="Transparent"
                 BorderBrush="White" BorderThickness="2" Opacity="1.0">
                     <Style TargetType="{x:Type Border}">
                             <Trigger Property="IsMouseOver" Value="True">
                                 <Setter Property="Effect">
                                         <DropShadowEffect ShadowDepth="0" Color="Gold" Opacity="1" BlurRadius="20"/>
                 <TextBox HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="4"/>
                 <local:HeaderedToolTip HeaderText="My first fantastic header."
                                              ContentText="Some tooltip text. This could get really long."
         <Grid IsEnabled="False" ToolTipService.ShowOnDisabled="True">
             <Border Width="180" Height="180"
                 Margin="10" Background="Transparent"
                 BorderBrush="White" BorderThickness="2" Opacity="1.0">
                     <Style TargetType="{x:Type Border}">
                             <Trigger Property="IsMouseOver" Value="True">
                                 <Setter Property="Effect">
                                         <DropShadowEffect ShadowDepth="0" Color="Blue" Opacity="1" BlurRadius="20"/>
                 <TextBox HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="4"/>
                 <local:HeaderedToolTip HeaderText="My second fantastic header."
                                              ContentText="Some tooltip text. This could get really, really long."

That's it. Here's a look at the tooltip in action. Please excuse my vector paragraph image in place of an actual Info icon.


Of course, in your actual implementation, data like the header's tooltip and contents will be driven from ViewModel data binding.

If you want to allow a custom icon for each use of the HeaderedToolTip, you would add an Image as another DependencyProperty on the custom control class and as another TemplatePart in the ResourceDictionary.

That's all for this WPF Tip. You can take any of these concepts and apply them to other custom controls you may need in your own applications. For more information about creating controls, check out Microsoft Docs.

Happy coding!

Saturday, October 20, 2018

Using the Rapid XAML Toolkit for Accelerating WPF Development (Preview)

Last week, I published an article on my UWP Tips blog about the Rapid XAML Toolkit.

The toolkit is for all types of XAML development, including UWP apps, Xamarin.Forms, and WPF. The toolkit is still in an early preview stage, so you will be doing a little bit of work to get up and running at first. For all the details, read my post and get started today!

Happy coding! Tags: ,,

Thursday, August 9, 2018

Can I Port My Windows Desktop Application to .NET Core? Find Out and Prepare Today!

Did you know that soon you will be able to build and run your WPF (and UWP & WinForms) applications against .NET Core?

Desktop Packs

At the Microsoft Build conference in May, it was announced that the .NET team was working on some Windows-only "Desktop Packs" that could be installed on top of .NET Core v3.0. These packs would include:

  • WPF
  • WinForms
  • UWP
  • Entity Framework v6

This will bring the benefits of .NET Core to desktop client applications running on Windows. In my opinion, the biggest benefits of porting your desktop app to .NET Core will be significant performance gains and full access to Windows 10 APIs.

Obviously, in most cases, it won't be as simple as changing a project property and rebuilding your app. Project files will need to be upgraded to .NET Core format, and some legacy APIs will not be available, even in the Desktop Pack packages. This week, it has become more clear exactly which APIs will not be available in .NET Core 3.0 with Desktop Packs.

Portability Analyzer

On Wednesday, the .NET team released a Portability Analyzer tool to scan your .NET desktop applications and determine how ready they are to upgrade to .NET Core 3.0. The tool will scan your .NET binaries and dependencies and produce an Excel report (PortabilityReport.xlsx) with several tabs:

Portability Summary - This sheet contains a list of the assemblies scanned, their current .NET version target, and the % compatibility they are expected to have with .NET Core 3.0.

Details - This sheet provides details on exactly which target type and member are currently used by one of your assemblies that is not compatible with .NET Core 3.0. There is a column for Recommended Changes, but none of my incompatibilities had any recommendations.

Missing assemblies - If your assemblies reference any other assemblies which could not be resolved by the tool, they will be listed here.

My Thoughts

I ran the tool on a large application with a mix of WinForms and WPF windows. The folder contains over 900 DLLs. My thoughts:

First, I was pleasantly surprised at how fast the tool ran its analysis. It completed scanning this large application in less than a minute. I was expecting to go get a cup of coffee and a snack while it ran.

Second, the team has really included a lot of desktop APIs in these packs. I was not expecting so many of my DLLs to report at 100% compatible.

Third, many of the incompatible APIs are somewhat expected. Here are a few that came back in the report here:

  • Microsoft.VisualBasic.* - If you have VB apps, and use APIs in this namespace, expect a lot of lines to appear in your report.
  • System.Drawing.Design.* and System.Drawing.ImageConverter - Much of the System.Drawing namespaces was ported to .NET Core, but not everything. If you're doing anything advanced or uncommon, you'll probably find some issues here.
  • Things that are less common in modern development like System.Xml.XmlDataDocument, System.Runtime.Remoting.Messaging, and System.Activator.GetObject.

Console Tool

If you would prefer to run the analyzer against several applications and product individual reports for each, you can use a console analyzer with a batch file or PowerShell script instead. That version is available here, and instructions for using it are in the .NET team's post about the Portability Analyzer.


Go get your app ready for .NET Core 3.0 today! Your users will thank you later.

Tuesday, May 29, 2018

TechBash 2018 Early Bird Tickets Now Available!

4-day (with Workshop) and 3-day early bird tickets are now on sale for TechBash 2018. This year TechBash will be held October 2-5, with the 2nd being a pre-conference day of full-day workshops for 4-day ticket holders only. These workshops will be presented by top experts in each area. The topics from which to choose are ASP.NET Core 2.1, DevOps, Docker and Azure for Developers. Get more workshop info here.

Early bird tickets will save up to 25% off the Standard ticket price this year. Don't wait because these prices will only be available for a few weeks and space in each workshop is limited. This year's keynote presenter is Scott Hunter, leader of the Visual Studio and .NET Teams at Microsoft, and these other speakers have already been confirmed:

  • Jeremy Likness
  • Jessica Deen
  • Richard Taylor
  • Jeremy Clark
  • Anna Bateman
  • Brendan Enrick
  • John Wright
  • Ashley Grant
  • Ivana Veliskova
  • Steve Bohlen
  • Angel Thomas
  • Jim Wooley
  • Walt Ritscher
  • Paul Hacker
  • Jeff Fritz
  • Mitch Denny
  • Anoop Kumar

The full lineup will be announced soon. Get more info about TechBash here and buy your tickets today!

See you this fall at the Kalahari Resort in the Poconos!

Thursday, November 30, 2017

WPF Tip #20 - Extended WPF Toolkit - IconButton Control

It's time to explore a new control in the Extended WPF Toolkit, maintained on GitHub by Xceed and the WPF community. Today, we will take a quick look at the IconButton control, which is new to v3.2.0 of the toolkit (released on Sept. 25th).

IconButton derives from a WPF Button and adds the ability to optionally place an Image to the left or right of your button's Content. There are a handful of properties available on top of the existing Button properties - Icon and IconBackground plus properties to change the button's appearance when users mouse over or press the button:
  • MouseOverBackground
  • MouseOverForeground
  • MouseOverBorderBrush
  • MousePressedBackground
  • MousePressedForeground
  • MousePressedBorderBrush
These state-based properties are self-explanatory, so I created a brief example which uses only the Icon and IconLocation properties.

<xctk:IconButton Content="Launch Me" Padding="4" 
                  HorizontalAlignment="Center" VerticalAlignment="Center" 
         <Image Source="/WpfApp1;component/Images/Launchpad_Icon.png" 
                Width="16" Margin="4,0,0,0"/>


The result is a window with a single "Launch Me" IconButton. There's an Apple-style launch icon to the right of the text set in the Content property. The image I used is a .png file in my project's Images folder set as a Resource. If you wanted to deploy the actual .png file, you would update the file to be "Content" and set the Image's Source property to "Images/Launchpad_Icon.png". This is the same practice you would use for any WPF image source.

All of these properties can be set up via MVVM, data binding and code in your ViewModels to load the image and set its location at runtime based on other application configuration or user preferences.

Here's is a look at the window at runtime:


It's a simple control that can save you a little bit of control templating. If this is something you could use in your project, the toolkit is free and available on NuGet.

Happy coding! Tags: ,

Thursday, September 7, 2017

WPF Tip #19 - Extended WPF Toolkit - Zoom in with the Magnifier Control

It's time to examine another control from the Extended WPF Toolkit, maintained on GitHub by Xceed and the WPF community. Today, we will look at an example of how easy it is to add a zoom feature to your WPF application with the Magnifier control.


In the Xaml below, I have added a Magnifier to the top-level grid in my Window.


         <tk:Magnifier BorderBrush="MediumPurple"
                               ZoomFactor=".4" />

    <tk:CheckListBox ItemsSource="{Binding Sentences, IsAsync=True}"
                      SelectedItem="{Binding SelectedSentence}"
                      Command="{Binding ItemSelectedCommand}"/>

The CheckedListBox from Tip #18 is still in place, displaying results from my Bacon Ipsum generator from Tip #17. Running the application, we see my random list items with a zoom region that follows my mouse as I move it around the Window.


Pretty simple! Go check it out for yourself. You can easily add a bound visibility property to hide/show the control from a toolbar or other button in your app.

Happy coding! Tags: ,,