WPF Control Grouping - wpf

I have a group of controls that look like this:
<Link to Image>
that i reuse a number of times. It's really simple a listview, 3 buttons and some layout panels.
I want to turn this into a reusable component but the columns in the listview can change and the sources they are bound to will change.
How do i go about this? i've seen many comparisons between ContentTemplates and UserControls etc but they never seem to be functional (eg Add will raise an event which i'll have to handle to add something to the listview, remove will raise an event where i'll likely ask if they are sure first).
I've accomplished the events with my own UserControl, but can't pass a list of GridViewColumns to the control. It also means i have to expose SelectedItem etc manually from the UserControl. Subclassing Listview seems promising for setup and access but doesn't conceptually seem right to have other controls in the listview area.
What is the right way?

I would definately recommend a UserControl. You should:
Add the controls you require to your user control
Add the Dependency Properties you require to your user control which allow you to configure it, e.g. SelectedItem
Wire up these dependency properties to the various controls within your user control. An easy way to do this is to set the DataContext of your user controls visual tree to the user control itself, e.g. if you have a Grid as the root for your user controls, set its DataContext = this in code. You can then use TwoWay bindings to connect up the various control properties to the user control properties.

Related

creating WPF user control with no interface

I would like to create a user control that you can drag and drop from the tool box to your XAML but that has not interface at all. I was used to do it for timers and background workers and this kind of elements.
So you allow users to drag into their projects and then set the name and other properties through the properties panel, attach functions to events in the event panel and they may perform almost all the set up without writing a single line of code (some customers love that)
Is that even possible ??
There is no "Component" type class like you might have used in WinForms, etc. However, you can probably just derive from FrameworkElement. By default it does no rendering so nothing will show up and as long as you don't set any Margins, MinWidth, etc. it shouldn't affect the layout really either. You might be able to derive from UIElement but using FrameworkElement will mean your object will have access to the DataContext so properties on it can be bound to things on it.

Locking controls on the canvas in a form editor in WPF

I am building a form editor in WPF. One of the features we need in it is to let the user "lock" one or more controls on the canvas. By locking I mean the user would not be able to move, resize or modify other properties of the control.
I have tried with ContentControl but did not get the results I am looking for.
I think you should use the IsEnabled property. Basic controls with interaction have this, and you can expose it on your custom controls as well and pass the property down.
If something is set with IsEnabled = false it will be grayed out and cannot be affected by user input.

In WPF (or silverlight or WP7), should a picture wall derive from ItemsControl or Panel?

Assume that I need to create a class called PictureWall, which will be used to show pictures.
Now I find that Panel and ItemsControl can both be used to hold Children elements. So should the class PictureWall derive from Panel? or should it derive from ItemsControl.
Note: This is not a real requirement, it's just a hypothetical question. The real question is: when should I create a subclass of Control (or ItemsControl) and when should I create a subclass of Panel?
Note 2: This imagined picture wall control is not to be used in one application only. It may be used by other developers. If it derives from Panel or ItemsControl, it'll expose the property named Children to other developers. So in this case, deriving from Control is a better idea, right?
Note 3: This imagined picture wall control has its own default way of loading certain pictures (for example, pulling pictures from a server) and it does not want this way to be messed around. If this is the case, then we should not inherit ItemsControl, right?
Panel is a container that is used to arrange its children. For example: Grid with a title and one button on the bottom and an image on center - Grid is very flexible to help you move stuff and arrange them when you change the size of window etc.
ItemsControl is a control that helps you with a collection of items. Let's take a concrete example: Listbox. You can very easly show a list of items, applay template to all of them, so on and so forth.
Control class is basically a UI element that can have its own template.
Note that, it is a way much better to define own UserControl, edit template or style of your PictureWall, insted of subclassing (there are many advantages, for example you can use Blend to redefine the style).
Edit:
# note2
If I were you I would make my own User Control to reuse existing controls to make what I want. If that won't be enough I would subclass Control.
[StyleTypedProperty(Property = "FooStyle", StyleTargetType = typeof(Control))]
public partial class MyDangControl: Control
{
...
# note3
This is a bad idea to combine all in one. You should split the logic that fetch the data form yout Picture Wall. For instance, user presses thumbnail to download the image and whole UI hangs. Horrible UX.
To be crystal clear, let me quote Pro WPF in C# 2010
Control
This is the most common starting
point when building a control
from scratch. It’s the base class for
all user-interactive widgets. The
Control class adds properties for
setting the background and foreground,
as well as the font and alignment of
content. The control class also places
itself into the tab order (through the
IsTabStop property) and introduces the
notion of double-clicking (through the
MouseDoubleClick and
PreviewMouseDoubleClick events). But
most important, the Control class
defines the Template property that
allows its appearance to be swapped
out with a customized element tree for
endless flexibility.
ContentControl
This is the base class for controls
that can display a single piece of
arbitrary content. That content can be
an element or a custom object that’s
used in conjunction with a template.
(The content is set through the
Content property, and an optional
template can be provided in the
ContentTemplate property.) Many
controls wrap a specific, limited type
of content (like a string of text in a
text box). Because these controls
don’t support all elements, they
shouldn’t be defined as content
controls.
ItemsControl
ItemsControl is the base class for
controls that wrap a list of items but
don’t support selection, while
Selector is the more specialized base
class for controls that do support
selection. These classes aren’t often
used to create custom controls,
because the data templating features
of the ListBox, ListView, and TreeView
provide a great deal of flexibility.
Panel
This is the base class for controls
with layout logic. A layout control
can hold multiple children and
arranges them according to specific
layout semantics. Often, panels
include attached properties that can
be set on the children to configure
how the children are arranged.
They both can be used to display elements, but really an ItemsControl offers much more functionality. In addition, an ItemsControl doesn't really display it's elements, it leverages a Panel to do that.
An ItemsControl can display a list of items, which may or may not be UIElements/Visuals. The items can be templated using a configurable DataTemplate, which ultimately determines how the item is displayed. In addition, then items can be bound to an observable collection so it will automatically update.
Neither of these features are supported by a Panel. DataTemplates can be used, but you have to manually create an associated ContentControl/ContentPresenter and add it to your panel.
Ultimately, their functions are different. A Panel is used to display UIElements/Visuals. An ItemsControl is used to display any type of data and apply templates as needed.

WPF - How to extend an existing toolbar UserControl

I create a new UserControl for reuse. The UserControl contains a toolbar with several Buttons.
When I use the UserControl, I hope I can add additional Buttons on it but can not modified the orginal ones.
This is easily implemented in WinForm by inheritance. But in WPF, I have no idea how to do it.
There is a few ways to do what you need.
For example, your UserControl could expose ICollection TabButtons property, which is ItemsSource for toolbar. In this case, your toolbar should contain some ItemsControl to host buttons and should be binded to aforementioned TabButtons property.
And TabButton struct should describe all needed information (for example, properties Header, Icon, ToolTip and Command).
Or you could provide appropriate methods: AddToolbarButton(), RemoveToolbarButton(). This methods change TabButtons collection (but the collection itself cann't be accessed from outer realm).

WPF patterns: Items and ItemsSource

Well I'm designing a Custom WPF control - fore the sake of learning - that display logs message in a similar way Visual Studio does. I want to allow the user add messages by adding message istances to an Items collection, or by binding to an ItemSource. I think this is a well established pattern in many wpf controls, but I have no Idea on how achieve it. I know I can obtain the same result by adding a listview as a part of my control, but the project goal is learning, so I prefer avoid that solution. Any idea ?
Have a read around the ItemsControl, your custom control can inherit from an ItemsControl, or a derivative of it. If you create an ObservableCollection containing your items and bind that to your ItemsSource, then your list will be automatically updated. You can style the ItemTemplate and Template to give the list a different look and feel.
There's loads of info here

Resources