Note in XAML

BitSmithy 1,956 Reputation points
2021-04-26T08:17:55.677+00:00

Hello,

I need to add to Canvas or other Panel an Info Property. This property shoud be a string.
But I dont want to create a new class wchich derives from Canvas.

Can I solve this task in any way.

Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. AryaDing-MSFT 2,916 Reputation points
    2021-04-27T06:43:40.783+00:00

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful