I want that some of the controls in my window will be surrounded by a border and a label above them that will contains a title, which tells something about a control (e.g. a list of persons, surrounded by a border, and above the list there is a label with the text "My Persons"). Since I want apply this on many controls, I don't want to write a specific xaml for each one of them. Should I create a user control, or is there a way to use styles/templates for that?
Visual Example:
http://dl.getdropbox.com/u/829214/example.GIF
I would look into adorners and the adorner layer.
Another way to accomplish this would be to create a custom/user control that derives from contentcontrol. Use a contentcontrol to vary the content and design the control to look however you want.
Related
At runtime I am creating a WPF control. It can be any WPF Control.
I want to find out whether this WPF Control, IS A CONTAINER CONTROL.
i.e. can this control can embed/contain another control.
Does there exist any Attribute through which the above can be achieved?
e.g. a label, panel, groupBox can contain other control like button, but textBOX cant.
In WPF there are a few base classes that controls extend from.
ContentControl: Most controls extend from this because most controls can store some type of single content within them.
ItemsControl: These are types of collection controls, which contain a collection of content within them.
Panel: These are special controls for layouts. They implement the ArrangeOverride and MeasureOverride methods to measure/arrange the children controls within them. Most of them also expose attached properties for best use.
That being said, you would typically want to use the is operator to check if your control inherits from what you intend it to. You can be specific or general, but typically you want to be as general as possible. I'm not sure what you meant by "container", which is why I listed the three (3) typical base controls that could contain other controls. For example:
if (myControl is ContentControl)
{
// Do something here.
}
All the controls mentioned here and here apart from TabControl inherit from Panel
So you can do something like:
if (myControl is Panel || myControl is TabControl)
{
// Then it's a container
}
I need to create a WPF custom control that will combine a label and a textbox, the label being on the left and the textbox on the right, side by side. I need that kind of control because I'm working on forms that all have the same pattern: label + field to fill. This control would have a LabelContent property and Text property or something like that.
Any advices on how to put that all together?
Thanks
You might want to use a UserControl instead of a CustomControl. It is designed to allow you to compose other controls into a single, reusable control.
Can someone elaborate the difference between ControlTemplate and DataTemplate in wpf?
What should one use in case of custom controls? Like for example a StackPanel which possibly has an image and a TextBox?
It seems confusing in some cases where you define a custom control using the 'Content' property.
It would be great if an example of how each can be used in different scenarios can be provided.
A ControlTemplate is used to change the look of an existing control. So if you don't want your buttons to look rectangular, you can define a control Template which makes them look oval or any irregular shape. It's a way to customize 'look-less' stock WPF controls ; an alternative to writing your own user-controls. More details
A DataTemplate is used to specify how an instance of a specific class (usually a Data Transfer object - an object with properties) is to be rendered visually. e.g. define a DataTemplate to visualize a Customer instance in a listbox displaying all customers. More details
I'd like to know what the differences between an Style (for a control) and a control template are.
Best regards,
Gonzalo
A style controls the individual properties of a control. For instance, a button style can say, "For every button, use this background." A style is changing a single property on a control.
A control template is actually handling how the control displays its bound data. Instead of saying, "I want to override a control's properties," you are assembling together other smaller controls into a single control that can present different views of the bound data.
Previously in WinForms, if you wanted to write a custom list box (say that had an icon next to each item), you had to inherit from the ListView control and override the painting methods. This involved a ton of experimentation - huge pain. With WPF templates, you can use XAML to construct smaller controls together and bind them to different properties of the parent control. You are actually defining the Visual Tree for the control.
See this article for an in-depth explanation by Charles Petzold.
Imagine your control is a house.
A Style is conceptually similar to putting down a new carpet and painting the walls. The house itself is still the same but its outward appearance is different.
A ControlTemplate is the equivalent of knocking down a wall or adding a conservatory. The actual structure of the house has changed.
Use a Style when you want to change the outward appearance of the control E.G. it's background colour or the thickness of it's border.
Use a ControlTemplate when you need to change the underlying structure of the control. This is useful when you want to change the layout of certain aspects of a control. A good example is in this article which re-templates a TabControl to look like the navigator from Microsoft Outlook.
I am adding user controls in my solution. The main page of my project will have a dock panel. When writing the xaml for my user controls should I remove grid and use dock panel or what? Iam using visual studio express and WPF.
The answer is it depends on what you want your control to look like.
Remember the Grid is to help you orgainze your controls. You can define features like columns widths, row heights, and other styling.
While a dock panel is to help dock your control to a part of the form or another.
So it depends on what you want your control to look like. You can nest a grid in a dock panel and vice versa. There is no set rule. It is really up to you.
Inside your user controls, use whatever type of panel makes sense for the contents of that control. In the main page, you set the DockPanel.Dock attribute on your user control, but that is independent of what's inside the user control.
It really depends on what you want to do.
Your top level may not even be a panel. For example, if you only want one control in the user control, then you really don’t need the panel.
Also, in some cases, even with composite controls, you may want to put something other than a panel as the top level control, for example, an expander.
Further, if you want to create a control that is very similar to another control, but behaves a little different, you may not even want to use a user control and instead inherit from an existing control. An example of this would be a numeric textbox that inherits from a textbox but adds keypress filtering.