The value of a custom dependency property from a derived control doesn't display on Property Editor of VS

Simos Sigma 1 Reputation point
2023-05-05T08:58:10.54+00:00

I am making a derived control, a ToolTip in my case, and I am facing an issue with its custom dependency properties. Every time I change the value of a custom dependency property to any other than default value, after I run my app and close it, into the Property Editor window of Visual Studio the field is empty.

This is a code example of my derived ToolTip control:

namespace MyControlLibrary.Controls
{
    public class MyToolTip : ToolTip
    {
        public enum IconTypes
        {
            Custom,
            Error,
            Information,
            None,
            Question,
            Warning,
        }

        public static readonly DependencyProperty IconTypeProperty =
            DependencyProperty.Register(nameof(IconType),
                                        typeof(IconTypes),
                                        typeof(MyToolTip),
                                        new FrameworkPropertyMetadata(IconTypes.None,
                                                                      new PropertyChangedCallback(OnIconTypeChanged)));

        [Category("MyCategory")]
        [Description("...")]
        public IconTypes IconType
        {
            get { return (IconTypes)GetValue(IconTypeProperty); }
            set { SetValue(IconTypeProperty, value); }
        }

        private static void OnIconTypeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            
        }

        static MyToolTip()
        {
            //DefaultStyleKeyProperty.OverrideMetadata(typeof(MyToolTip), new FrameworkPropertyMetadata(typeof(MyToolTip)));
        }

        public MyToolTip()
        {
            
        }
    }
}

And this is how I am testing it into MainWindow:

<Window
    x:Class="MyApp.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"
    
    xmlns:local="clr-namespace:MyApp"
    xmlns:MyControls="clr-namespace:MyControlLibrary.Controls;assembly=MyControlLibrary"
        
    Title="MainWindow"
    Height="450"
    Width="800"
    WindowStartupLocation="CenterScreen">

    <Grid>
        <Button
            Content="MyToolTip Test"
            Cursor="Hand"
            Padding="9"
            HorizontalAlignment="Center"
            VerticalAlignment="Center">
            <Button.ToolTip>
                <MyControls:MyToolTip IconType="Custom">
                    <StackPanel>
                        <TextBlock Text="MyToolTip Test!!!"/>
                    </StackPanel>
                </MyControls:MyToolTip>
            </Button.ToolTip>
        </Button>
    </Grid>

</Window>

Also here is a VS solution which reproduces the issue!!!

Any idea why is this happening?

UPDATE 06/05/2023:

This issue happens when derived control is nested within a ToolTip!!!

I created a different derived control, a TextBlock for example, with a custom dependency property, I put it into <Button.ToolTip>...</Button.ToolTip> and I have the same issue. But if I put it out of <Button.ToolTip>...</Button.ToolTip>, issue ceases to exist!

Looks like something is wrong with the serialization of custom dependency properties values, because when ToolTip displayed, a new Popup control is created to host the contents of the ToolTip and this Popup control is not part of the visual tree of the MainWindow and therefore any bindings or property values set on the controls inside the ToolTip may not be properly serialized. Or something like that.

Developer technologies | Windows Presentation Foundation
Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,711 Reputation points Microsoft External Staff
    2023-05-08T02:51:04.5633333+00:00

    Hi,@Simos Sigma. Welcome Microsoft Q&A.

    After my test, when the custom control inherits Control its custom enum dependency property can display the value set from the code.

    When the custom control inherits Tooltip its custom enum property does not display the value set from the code.

    It is recommended that you choose to set its value in the property editor.

    Default values for dependency properties are displayed correctly.

    User's image

    When we add this custom control to our WPF project and open the MainWindow in the Visual Studio Designer, we should see that the IconType property is displayed in the Property Editor and has a dropdown list with the values Custom, Error, Information, None, Question and Warning. We can select any of these values to set the value of the property for the MyToolTip instance.

    7


    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.


Your answer

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