Create custom user controls in wpf for reuse - c#

I was wondering how could I save a user control just like we can save a c# class in a dll file and import that file whenever we need to use that class. How could we import that control and use it. It will be nice if we could make custom controls and save them for reuse instead of having to copy the xaml and code behind every time we had to use it.

You have exactly that flexibility with WPF and a lot of choices. Here is an excellent introduction to your options:
Control Authoring Overview
These choices include:
Deriving from UserControl
Deriving from Control
Deriving from FrameworkElement
The tradeoffs are described in detail in the article.

Related

How to Share a Winforms Control and Code Behind Between Different Forms?

I've created a groupbox that is used to populate a list of files and their description. I've also written the code behind for properly adding the files. Is it possible to share the code behind and UI so that it can be used in other forms without having to rewrite the code? I know that you can create a base form, that contains the winforms control you want to share. That way you can have your new form you inherit from the base class. But I'm not looking to implement it this way.
YES, it can be. Create a User Control for your GroupBox and use the same in whichever form you want. User controls are meant for that purpose to increase code re-usability.
In case, you think the same GroupBox control can be used in other project as well then you should consider building it as Custom Control

Customize control/ Writing Control Templates in wp7

I have been trying to customize a webbrowser control in wp7. All I need to do is to add a border property(I think it is already available in webbrowser but I have to make it mandatory when I make it as a control) and want to add a few extra event handlers and make it as a control so that it can be used later in different projects.
I tried reading through materials in msdn for writing control template and it all revolves around writing a few xaml code and attaching it with codebehind to make it work. What I don't understand is how do I learn xaml? or in other words where is the reference for all the tags that msdn talks about in xaml? Itseems to be huge and I am not sure how to go about it. The tutorial in msdn straight away divulges into xaml code and I am totally confused..
So now all I am trying to do is this,
create a control in Expression Blend (Windows Phone Control Library) that would create a class (MyCustomControl) that inherits
from CustomControl base class.
Now in xaml I am adding a webbrowser control and adding four event handlers (mouseup,down etc.,).
I build this control in blend and add the corresponding dll in VS2010.
And now once I try to add this control to my wp7 phone application it says "Cannot create an instance of MyCustomControl"..
These were further links that I referenced in creating one,
Windowsphone - Creating a custom control
Windowsphone - Control Template.
UserControl vs Custom Control
Creating a new control by creating ControlTemplate
Any further help would be great.
There is no definitive list of 'elements' you can add to your XAML. The reason for this is that the XAML parser can create any class which is a UIElement based on the XML you provide. So the elements available to you depend on the assemblies present in your project. Read teh MSDN XAML Overview for details
For a list of controls that are present by default, take a look at the System.Windows.Controls namespace (I think this link is not for your version of Silverlight, it might be best to use the Object Browser to look at the assemblies in your project).
For your problem, where you want to add a border to a WebBrowser control. I would recommend creating a UserControl as per this tutorial.

Difference between User Control and Custom Control Library [duplicate]

This question already has answers here:
What is the difference between a User Control Library and a Custom Control Library?
(3 answers)
Closed 1 year ago.
I'm working on creating a date/time user control in WPF using C# 2008. My first user control. I'm also using Matthew MacDonald's book, "Pro WPF in C# 2008". In that book he strongly recommended creating a user control using the WPF Custom Control Library project template; so I followed his suggestion. I've finished writing the code which would go into what I think of as the code-behind file. Now I'm ready to write the XAML.
The only problem is, I just discovered there is no corresponding .xaml file? So, I don't get why using a WPF Custom Control Library project is better, or prefered, when writing a user control?
A user control and a custom control solve two distinctly different problems.
UserControls are meant to compose multiple WPF controls together, in order to make a set of functionality built out of other controls. This is often used to compose a portion of a window or screen in order to organize your development by allowing you to group multiple pieces of functionality into one "control". For example, if you wanted to make a control for editing a User which provided text boxes for first and last name, age, etc., a single UserControl could be dropped onto a Window and bound to a User instance to edit this. (In this case, you're using standard controls, such as TextBox, to "compose" a control for a more complex purpose.)
A CustomControl, however, is meant to be a new single control. This would typically be a replacement for a built-in control (which could not be redone via templating). I've found that the need for CustomControls is actually fairly rare in WPF, since the WPF templating options and attached properties allow you to do nearly anything with standard controls, once you learn them fully.
I would also add, if you're intending to inherit from your control, then using a usercontrol is will complicate matters. For example, if create a base usercontrol that has a layout defined in Xaml, WPF framework won't allow you to inherit this control and use Xaml to define the layout for the subclass. In dotnet 3.5 Xaml control can't inherit from another Xaml control

Creating Custom Controls with .ascx files

I am looking creating some custom controls that need to be compiled as a stand alone dll rather than with the primary application. Unfortunately so far this has meant manually creating the controls without the use of the designer since afaik you can't compile a User Control as a stand alone component. As doing things this way makes the controls rather difficult for the designers to style without putting in a lot of effort on the developers part, I am wondering if there is in fact a viable way to compile a .ascx style control into a stand alone binary?
You can turn a user control into a custom control that is in a standalone .dll, but it loses the ability of being updateable via markup.
In other words, you can build an .ascx file using markup and code, and then turn it into something similar to a control that was built exclusively with code.
Turning an .ascx User Control into a Custom Control
You have to understand that user controls are just a convenience for reusable content though - ultimately, the markup does get processed and compiled. Custom controls are really the only way to distribute a web control. IF you have to distribute controls as a library, I would concentrate on making them CSS friendly for your devs with just a few layout controlling properties, rather than messing around with .ascx.
The MSDN article http://msdn.microsoft.com/en-us/library/aa479318.aspx explains how you can do this. Basicly you precompile the application and take the resulting DLL.

What is the difference between User Control, Custom Control and Component?

These are three different things you can add to a project and I am not quite sure if I understand the difference. They all seem to for example show up in the component toolbox when working with a Form. What are some common usage scenarios for each of them? What is the difference?
The main difference between User Control, Custom Control and Component is that they inherit from different levels in the inheritance tree:
MyComponent
|-> Component
MyCustomControl
|-> Control
|-> Component
MyUserControl
|-> ContainerControl
|-> ScrollableControl
|-> Control
|-> Component
So, in short you get a different amount of pre-wired functionality with the different options.
When would you use the different options? (these are thoughts and opinions, not truths)
Create a component if you want to provide functionality without UI (such as Timer components, data sources, ...)
Create a custom control if you want to make a component where you have full control over its visual appearance, and you don't want any baggage of unnecessary functionality. Typical cases would be simple controls with limited functionality (such as a button)
Create a user control if you are going to combine existing controls into reusable building blocks (such as two lists with buttons where you can move items between the lists).
Adding to what Fredrik said, generally components and custom controls would be used when you plan on reusing them across projects. Use user controls when you would only be using it in one project.
I believe the last statement is not correct in my opinion .
I create user controls for many different reasons.
The main reason is so that if per say I design an interface of multiple controls
grouped together.
I first create a class library , then I add user controls to it .
Now if i need to change any part of the logic behind how the user control works I can very easily. Also this class library can be used multiple times.
Also within the same classy library I can have multiple classes that can be shared and used for any of my user controls.
This is the main reason I use user controls.
And if you make a change to your user control or class library .
once you build the job .
the dll will dynamically up date in the bin folder .
So if i am referencing this in another project
Those changes will also appear in new project .
Also it doesn't use the same paint routines as the form and anything you have loaded on the form.
So user controls gives us the ability to be very modular
And i Can have multiple user controls that share the basics classes of the class library ...
So a user control purpose is just not for one project . It has no limitations in that respect.
jeff
The main difference between them-
User Control is a page file with extension .ascx which can only be
used within a single application or project But custom controls are assemblies(dll files) that can be used in multiple applications.

Categories

Resources