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.