WPF DependencyProperty Not Working with .Net Core

R Evans 211 Reputation points
2020-11-02T21:36:42.423+00:00

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.

36857-image.png

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

36913-image.png

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);  
        }  
    }  
}  
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,765 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,621 Reputation points
    2020-11-03T03:23:00.93+00:00

    You need to choose Content for Build Action and Copy always/Copy if newer for Copy to Output Directory on **Properties ** for the images like the below picture shown:
    36920-capture.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.