Click here to Skip to main content
15,886,693 members
Articles / Desktop Programming / XAML
Tip/Trick

How to Create and Use Custom Control in UWP

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
1 Dec 2015CPOL 29.5K   3   4
How to create a Custom Control so it can be used later and how to give access to some of it properties
  • Create a new project. Add a new item and then select user control.

    Adding User Control

  • Then go to MyUserControl1.xaml and add textblock and image from toolbox.

    User Control

  • Now, we want to give the control to the user using our custom control so he/she can change text and image. For that, go to MyUserControl1.xaml.cs and add a dependency property of type as of the 'text' property of textblock, i.e., string. In this case, set metadata to 'null'.

    Dependency Property

  • Do the same for source of the image of the type 'ImageSource'.

    Dependency Property

  • Now, bind the respected properties with our dependency properties so they can be changed by the user.

    Data Binding

    Data Binding

  • Now, we can use our created custom control anywhere we like. And can change the text or image source to add any image.

    Using Custom Control

  • Like this, we can give the desired control over our custom control to the user by binding them with our created dependency properties.

Source code: https://github.com/umerqureshi93/CustomControl

Personal blog: http://csdebugger.blogspot.com/

License

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


Written By
Student
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow can I consume UWP usercontrol in my MFC application? Pin
srajesh_nkl25-Sep-19 15:35
srajesh_nkl25-Sep-19 15:35 
QuestionGood stuff - Out of Date in VS 2017 Pin
Larry Aultman6-Nov-18 9:10
professionalLarry Aultman6-Nov-18 9:10 
QuestionMy Vote of 5 Pin
aarif moh shaikh2-Dec-15 19:13
professionalaarif moh shaikh2-Dec-15 19:13 
AnswerRe: My Vote of 5 Pin
Umer Qureshi6-Dec-15 6:55
professionalUmer Qureshi6-Dec-15 6:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.