Custom Controls in Blend 2015
Introduction
Now we can create our own custom controls in Blend for Visual Studio 2015 , so let’s see how can we make a simple custom control.
Example
I am making a custom control of a “sign up page ” and for this I am gonna need a
- Image ( image property un assets)
- Email or user name (text block)
- password (password block)
and once I create a control , the benefit is that I can just simple drag and drop that control anywhere I want to use it in my application ,without starting from a scratch so lets see how Blend 2015 can help us in making a simple custom Control
Steps
The simple steps of creating a custom control are mentioned below.
New Project
Simply create a new project in Blend 2015
https://zainnisar237.files.wordpress.com/2015/12/1.png?w=700
Select Template
- Select Universal
- Select Blank App.
- Name your solution , for instance “CustomControl”.
https://zainnisar237.files.wordpress.com/2015/12/2.png?w=700
Add New Item
- Go to tool bar .
- Select Project.
- Select Add new item from the project drop down menu
- Select User Control.
- Name it ,for instance DesignControl.
https://zainnisar237.files.wordpress.com/2015/12/3.png?w=700
User Control
A new User Control is added now , work on it , just select all the items you needed and drag and drop on the window , in my case as I mentioned I need three things and an additional rectangle , all my features will be in this rectangle which will act like a grid.
- Select a Rectangle from the Tool Bar and drop on Design Control.XAML
- Select Image from the Assets and drop on Design Conrol.XAML.
- Select Text Block from Tool Bar and drop on Design Control.XAML.
- Select Password from your Tool Bar and drop on Design Control.XAML.
- You can also change name for our own convenience , like I did in Objects and Timeline window.
https://zainnisar237.files.wordpress.com/2015/12/4.png?w=700
Dependency Property
In your next step , simple add dependency property the code snippet of which is writing propdp and press tab twice. It will appear like this on your Design Control.XAML.cs window.
https://zainnisar237.files.wordpress.com/2015/12/5.png?w=700
Dependency Property For Each Control.
For your each control you add in your DesignControl.Xaml you have to add a dependency property for it in Design Control.Xaml.cs and then change the attributes , you can take the help of the pic .
- Go to your DesignControl.Xaml.cs window.
- write propdp and press tab key twice.
- do the above step for each control.
- In my case as I have four controls(rectangle ,image , textblock, password) I will add four dependency properties ,each for each control.
- Assign values to your dependency properties.
https://zainnisar237.files.wordpress.com/2015/12/6.png?w=700
https://zainnisar237.files.wordpress.com/2015/12/7.png?w=700
When you are done , press the Ctrl +S , Ctrl+shift+B.
Data Binding
Before moving to Data Binding , if you see a red line appear under your email or password in your dependency property there is no need to worry about …all you have to do is change the name of your control in you object and time line window.
Before
https://zainnisar237.files.wordpress.com/2015/12/4.png?w=700
After
https://zainnisar237.files.wordpress.com/2015/12/8.png?w=700
Hopefully the issue will ne resolved .:)
Data Binding for Controls
Time to do Data Binding for each control so lets begin , steps include.
Rectangle
- Select Rectangle ,in Object and TimeLine window.
- Select Fill in properties window ,right click the small box next to it.
- Go to Create DataBinding.
- Select Element Name.
- Select User Control.
- Select MyBrush.
- press ok.
https://zainnisar237.files.wordpress.com/2015/12/8.png?w=700
https://zainnisar237.files.wordpress.com/2015/12/9.png?w=700
Image
Steps will be same every time , here you go.
- Select Image .
- Select Source from Property windows , right click the small box.
- Choose Create Data Binding.
- Select Element Name.
- Select User Control.
- Select Image Source.
- press ok.
https://zainnisar237.files.wordpress.com/2015/12/10.png?w=700
https://zainnisar237.files.wordpress.com/2015/12/11.png?w=700
TextBlock
- Select your textBlock=EmailBlock.
- Select Text from property window , and right click the small box.
- Select Create Data Binding.
- Select Element Name
- Select User Control
- Select Email
- press ok
https://zainnisar237.files.wordpress.com/2015/12/12.png?w=700
https://zainnisar237.files.wordpress.com/2015/12/13.png?w=700
Password
- Select Password.
- Select Password from the properties window , right click the small box.
- Choose Create Data Binding
- Select Element Name.
- Select User Control.
- Select Password(string).
- press ok.
Now simply Build your solution by pressing Ctrl+Shft+B.
Main Page.Xaml
Go to Main Page.Xaml.cs
Select Assets.
Select Controls.
Check if your control is available.
DesignControl is there.
Drag and drop the control on
https://zainnisar237.files.wordpress.com/2015/12/16.png?w=700
Using Custom Control
Simply use that custom control you just created in your Main Page.Xaml.cs.
Add image if you have any from your Assets folder.
Add the Email and password in property window.
https://zainnisar237.files.wordpress.com/2015/12/17.png?w=700
Layout and Design.
You can also design and layout your custom control.
https://zainnisar237.files.wordpress.com/2015/12/18.png?w=700
Conclusion
The advantage of the Custom Control is that next time you don’t have to start from a scratch , you can simply drag and drop your control as many time you want to and can also customize it ,by adding any feature you want to .Happy Coding :)