Hi,
Welcome to Microsoft Q&A!
If you don’t want to create a new class that derives from Canvas, you could register a customer attach property like the following.
public class AquariumService : DependencyObject
{
public static readonly DependencyProperty InfoProperty = DependencyProperty.RegisterAttached(
"Info",
typeof(string),
typeof(AquariumService),
new PropertyMetadata(string.Empty)
);
public static void SetInfo(UIElement element, string value)
{
element.SetValue(InfoProperty, value);
}
public static string GetInfo(UIElement element)
{
return (string)element.GetValue(InfoProperty);
}
}
Setting property from XAML markup:
<Canvas x:Name="myCanvas" local:AquariumService.Info="hello,hello" />
Setting property from code behind:
AquariumService.SetInfo(myCanvas,"hello");
More info could be found here.
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.