What WPF control should I inherit from? - wpf

I've written a WPF control which accepts a number of UIElement objects as input and displays them docked either vertically or horizontally. The control exposes functions for enumerating, removing and inserting children, but internally I'm using a Grid to build the layout, creating a row/column for each item and inserting a GridSplitter between them. To do this I've inherited from ContentControl, and upon initialization I just set the Content property with the Grid. Everything is working as intended, but now I wonder if this might be confusing for the user of my control, as it would be counter-intuitive to have a ContentControl that has many items.
Should I be inheriting from ItemsControl instead?
Should I inherit directly from Control which is "content-agnostic"?
Is there a better way to do this?
Thanks in advance.

What you describe sounds like a Panel - basically a control which is responsible for the layout of many elements. I would consider inheriting from that.

Your mention of "docked either vertically or horizontally" leads me to believe perhaps a StackPanel would be a better fit, since it does docking as well.

Related

WPF Datagrid Inside WrapPanel?

I have a lot of columns in my DataGrid and would like to be able to put it into a wrap panel so it has the ability to resize while still displaying all of it's contents. Here's a visual example:
No WrapPanel
With WrapPanel (This is just two DataGrids for representation)
Is there any way to achieve this effect?
No, you can't get the DataGrid to wrap like you want it to.
To achieve the effect that you want would require creating a custom control.

Problems writing too much custom controls in WPF

I am working on new WPF/MVVM project, where I see all most all controls are written for different needs, right from textbox to treeview. All are rewritten for simple need, for example, grid,stackpanel control are rewritten to add space between each item and textbox is rewritten to include label for it so that it has both label and text input are on itself.
My question : Is there any serious issue we would encounter because of this customization?
Already, I am seeing issues with aligning all controls, will i would see any more issues because of this?.
You should never create a custom or user control to add margins, a label to a TextBox, or a new ItemTemplate to a ListBox.
UserControls are for grouping frequently used combinations of controls into one reuseable control. An example may be a custom List-of-Values control that opens a dialog. This would be fine to implement as a UserControl.
Custom controls are good when a native control does not suit your needs. Say that you want to reimplement a DateTimePicker from scratch because the native one doesn't include milliseconds.
There are no serious issues as such, but you may find yourself maintaining all these controls for the next many years without there being a need to do so.
Settings Margins should be done in the View where you are using it, or on a Style in a ResourceDictionary.
This is of course only my opinion (and that of many others, I except), but if you find that the majority of your controls are 'customized' this way, you are doing it wrong.
Style and Templates rather than UserControls and custom controls.
The main issue is that you lose the ability to alter margins only in a single view. If you change your custom controls' inner paddings and margins, you will be changing all the views in your solution. If you use a style, you can always override it by defining a new style in the view, or by setting the property directly.

datarepeater like control in Silverlight?

I need some kind of control to wrap my UI (which generates using binding). Currently I use ListBox but not sure if it's lightest or best choice. I just need placeholder that I can bind to and insert my controls.
You're probably looking for the ItemsControl
You can use any control that takes a list of entities as it's data source.
This could be DataGrid, ListBox (as you are already using) or anything that inherits from ItemsControl - this includes things like the TabControl as well.

WPF: ArrangeOverride in Customized StackPanel

I want to make a custom Panel to (amongst other things) insert spacing between all children. To inherit from Panel seemed very complicated and perhaps unnecessary. What I want is close to StackPanel so I figured I could inherit from StackPanel and modify the ArrangeOverride and MeasureOverride to get what I want.
I got the implementation of these from Reflector but immediately noticed that one property was not accessible at all from a inherited class, namely IsScrolling (and the field _scrollData). My question is if I can skip this or if there is any other way of implementing identical behaviour? Im not sure I will need scrolling for this custom panel but I may do some day and I dont want to paint myself into a corner...
if all you want is just that all elements inside a container would automatically contain margin definitions, you're much better off just creating a custom behavior that implements that functionality.
to create a custom behavior, just implement Behavior<Panel>. It's rather self-explanatory, but if you don't know how then ask.

When writing the xaml for my user controls should I remove grid and use dock panel or something else?

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.

Resources