Multiple Control Templates for a custom control in Silverlight - silverlight

I am creating a custom control. The contents of the control will differ a lot when in different visual states.
Can I to achieve the above, apply different control templates to the same custom control? That is define more than one control template for a custom control? If not, any clues as to how I can do this, without have as many custom/user controls as there are states?

You could define visual states inside a single ControlTemplate and also transitions between those states. Look for 'VisualStateManager'.

Related

Controls from external library not appearing in Telerik project

I'm working on a project that uses Telerik WPF controls (in so called NoXaml mode) and I have a library with controls I would like to use. These are simple WPF UserControls without even any template and contain only standard WPF controls placed inside them (and some logic). All these controls used inside are covered by Telerik templates and work outside of these UserControls. The library works everywhere, except in this Telerik project, in which the controls just don't appear, they are blank. Even a simplest <TextBlock> inside such control is invisible. Drilling down with Snoop sometimes makes them magically appear.
I've read that Telerik projects need to define templates for any custom controls that you create in them, but I'd like the library to be in control of the look of these controls, so my question is:
Can I actually use a custom UserControl library in a Telerik project without having to write my own control templates for them? If so, how to do that? If not, any other simple workarounds?
Thanks.

Which element to use to modularize WPF applications?

Lets say I have a search box, which is a stack panel containing a TextBox and a Button with an icon. For easier re-usability I would like to extract said search box into a separate file.
What would I use to wrap the searchbox? I have all the functionality as attached behaviors, so I don't need any code behind.
ItemsControl doesn't fit, because I don't want to display items, ContentControl does not, because I have no content...
Could you give me hints how to fragmentalize in XAML? The only examples I find are for ResourceDictionarys, but not everything is a Style.
User Control
User Controls provide a way to collect and combine different built-in controls together and package them into re-usable XAML. User controls are used in following scenarios:
If the control consists of existing controls, i.e., you can create a single control of multiple, already existing controls.
If the control doesn't need support for theming. User Controls do not support complex customization, control templates, and difficult to style.
If a developer prefers to write controls using the code-behind model where a view and then a direct code behind for event handlers.
You won't be sharing your control across applications.
Custom Controls
A custom control is a class which offers its own style and template which are normally defined in generic.xaml. Custom controls are used in the following scenarios:
If the control doesn't exist and you have to create it from scratch.
If you want to extend or add functionality to a preexisting control by adding an extra property or an extra functionality to fit your specific scenario.
If your controls need to support theming and styling.
If you want to share your control across applications.
source (including example and more)
What would I use to wrap the searchbox?
A UserControl: https://msdn.microsoft.com/en-us/library/system.windows.controls.usercontrol(v=vs.110).aspx
Creating a UserControl is a suitable model if you want to build your custom control by adding existing elements like for example StackPanels and TextBoxes to it.

using custom controls instead of user controls to create complex views

I do not have enough information about WPF, so please correct me.
It seems that to handle different views create many usercontrols is needed(each view needs one usercontol which binds to the viewModel) , and also by using MVVM pattern designers can create views independently.
now if the designer tries to create two themes with different structure, he has to create two usercontrols because when using usercontrols the layout is specified(as mentioned here).
on the other way customControls do not specify the layout, so it seems that using CustomControls is more reasonable.
so the question: is using custom controls instead of usercontrols is correct, and if it is, is it reasonable for viewmodels to inherit from Control, and views become only styles for viewmodels?
Unless you need the functionality provided by custom controls, I would suggest using UserControls or DataTemplates. They are simpler. Here's a related question\answer.
WPF User Controls vs Custom Controls

WPF User control or Template? How would i create a Template using Blend?

How would i create a window template using Blend? My application will be sharing many pages with the same layout.
One of my major concerns is what will happen when i need to make a change to the template. Will this update all of the pages automatically? Should i just use a usercontrol and add it to everypage?
Stay away from UserControls, generally. Instead, you want to use ControlTemplates, and put them in Resource Dictionary files, which you can totally make with Blend.

What is the best practice for creating RE-USABLE control templates in Silverlight

What is the best practice for creating re-usable control templates.
For example. I want to start with the standard checkbox and modify its template for re-use accross multiple future projects.
I understand how to modify the template in Blend, but it always wants to save the template to App.xaml of the current project or to the parent control where I first placed the checkbox.
Ideally I would like some soft of ControlLibrary that contains all my custom controls AND custom templates (modified templates of existing controls)
-Jeff
Implicit Style Manager might be helpful.
The Silverlight controls team and others have been blogging a bit recently about ISM which allows alternative style sets to be applied across the board to standard controls. Try Jesse Liberty's blog or Mehdi Slaoui Andaloussi's blog
HTH
I don't think you can have a ControlTemplate as a separate entity, without it being attached to a Control. For your control library you can create custom controls that have the appropriate custom templates and reuse the controls.

Resources