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.
Related
I have made one custom user control (search text box), which basically consists of one dock panel, and in the panel there are two controls: textbox and button.
I have set some default appearance for this control, which consists of setting a border on the user control, and setting no borders and no background on the textbox, and I have created a custom style for the button. So far this control looks the way I want, and is working correctly.
The problem arises when I want to allow themes for my application. When I put custom styles for basic windows controls (Button, TextBox, ListBox, etc), they are displayed properly with the new theme. But I don't know how to make a template for my custom control. Any attempt ended up in the control not showing at all at runtime.
What exactly must be done in the new template for this control? Do I need to make a template for the User control, and for the textbox and the button controls also?
You are mixing up user controls and custom controls here. What you described above is a user control, and you cannot apply new templates to it.
The States pane in Blend 4 does not appear to show the VisualStates declared on a UserControl using the TemplateVisualState attribute. Is this to be expected and if so why? Creating the States manually in Blend seems to work correctly.
User controls aren't typically designed to be modified or re-templated the same way that regular controls are.
Most of the time where you want a control to be editable and offer alternative animations (through the states panel for instance) you're going to want to build something that derives from a proper custom control type.
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 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.
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.