Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working on "Visual Studio 2022" and using ".NET Framework 4.8.1" 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" of "Visual Studio" the field is empty!!!

What I have tried:

This is a code example of my derived "ToolTip" control:
C#
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":
XML
<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>
                    <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?
Posted
Updated 5-May-23 0:50am

1 solution

Here is an example of a custom tooltip: Control ToolTips - The complete WPF tutorial[^].

If you want to extend a control, like a tooltip, then there is more than adding a DP like that. I have written an article on extending the ToggleButton to make a ToggleSwitch control: Flexible WPF ToggleSwitch Lookless Control in C# & VB[^]. Download, try it out, read the article and use it as a reference to roll your own custom control.
 
Share this answer
 
Comments
Simos Sigma 6-May-23 3:05am    
I read all your links very carefully. All those things are really useful and thank you very much. I'll surly keep all this as a reference. But in my case I think something else is wrong!!! What do I mean?

I think that all this, the issue with the field of the DP which is empty in VS "Property Editor" after I start and close my application, is happening because my custom control is nested into < Button.ToolTip > for some reason. Even if, for example, I create an other derived control, a "TextBlock", with a custom DP and put it into < Button.ToolTip >, I have the same issue. If I put my derived control out of < Button.ToolTip >, issue ceases to exist!!!
Graeme_Grant 6-May-23 5:15am    
Maybe this article will help you: Writing Custom Control with New WPF XAML Designer[^]. It's a bit of work for little return unless you are looking at marketing a custom control to the masses...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900