I've looked at using a control template to use the same XAML code between two views. However, where do I store this Control template so both views can access it and therefore display the controls I need?
Thanks
If both views are under same window, you can store ControlTemplate under Window.Resources and if both view belongs to different windows store that under Application.Resources.
Related
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.
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
lately i often see questions regarding MVVM and usercontrol, where - for me - view and usercontrol are mixed up.
for me a View when doing MVVM is just a pretty interface that allows users to interact with my ViewModels - so at least a collection of controls with Bindings. most time xaml only but codebehind not forbidden.
for me a usercontrol instead is not related to a viewmodel at all. the usercontrol has a representation in xaml and of course codebehind where properties, methods and Dependency Properties exist.
i'm on a wrong way with that?
EDIT: of course view and usercontrol inherit from UserControl class - so both technically UserControls. but i use just the term View when doing MVVM. and the term usercontrol just when there is no direct relation to a viewmodel.
ps: my english is too bad to write down what i mean...
You're not wrong just consider the fact the user control can be a reusable view that has a view model. cause the entire composite ui architecture is based on s shell(main window, view) and some regions with view (user controls)
A View and User Controls are totally different in MVVM.
The View is a conceptual name of the folder where you put all UI related stuff like user controls, windows, pages, etc. So the View is the folder that contains your GUI part for the particular application.
The User Control is the control which is configured by the developer by mixing multiple components / controls to work like a single control. A user control can also hold the other user controls.
The mix point is that, generally, views hold the user control in an MVVM application, as WPF is XAML based. It gets rendered in there, so the developer can plug your, his and other's user control into some where he wants to.
Whereas windows can not be placed into other windows. And pages can only be shown in frame element, so most views are user controls.
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'.
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.