For years, I have had the benefit of using code from an excellent Code Project article submitted by David Veeneman. You can view the article and download the code at https://www.codeproject.com/Articles/49802/Create-a-WPF-Custom-Control-Part-2.
The code for Outlook2010TaskButton works well when I converted to .NET Framework 4.8, but not so much when I ported the code to .NET Core. When I run in debug mode via .NET Core, the images in are not found in the Images folder. The error message is: ... Inner Exception ... IOException: Cannot locate resource 'images/calendar.png'. This exception was originally thrown at this call stack: [External Code]
My .NET Core solution consists of two projects: 1) WpfDependencyPropertyExperiment, and 2) WpfDependencyPropertyExperimentDemo. Both target .Net Core 3.1.
<Window x:Class="WpfDependencyPropertyExperimentDemo.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:WpfDependencyPropertyExperimentDemo"
xmlns:wpfDependencyPropertyExperiment="clr-namespace:WpfDependencyPropertyExperiment;assembly=WpfDependencyPropertyExperiment"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<wpfDependencyPropertyExperiment:TaskButton x:Name="Button1" Image="Images/calendar.png" Text="Calendar" Margin="2" Background="{Binding Path=Background, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
<wpfDependencyPropertyExperiment:TaskButton x:Name="Button2" Image="Images/contacts.png" Text="Contacts" Margin="2" Background="{Binding Path=Background, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
<wpfDependencyPropertyExperiment:TaskButton x:Name="Button3" Image="Images/project_list.png" Text="Projects" Margin="2" Background="{Binding Path=Background, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
</StackPanel>
</Grid>
</Window>
namespace WpfDependencyPropertyExperiment
{
public class TaskButton : RadioButton
{
public static readonly DependencyProperty ImageProperty;
public static readonly DependencyProperty TextProperty;
static TaskButton()
{
// Initialize as lookless control
DefaultStyleKeyProperty.OverrideMetadata(typeof(TaskButton), new FrameworkPropertyMetadata(typeof(TaskButton)));
// Initialize dependency properties
ImageProperty = DependencyProperty.Register("Image", typeof(ImageSource), typeof(TaskButton), new UIPropertyMetadata(null));
TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(TaskButton), new UIPropertyMetadata(null));
}
[Description("The image displayed by the button"), Category("Common Properties")]
public ImageSource Image
{
get => (ImageSource)GetValue(ImageProperty);
set => SetValue(ImageProperty, value);
}
[Description("The text displayed by the button."), Category("Common Properties")]
public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
}
}