jamielennox
jamielennox

Reputation: 388

WPF Control Grouping

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?

Upvotes: 1

Views: 981

Answers (1)

ColinE
ColinE

Reputation: 70142

I would definately recommend a UserControl. You should:

  1. Add the controls you require to your user control
  2. Add the Dependency Properties you require to your user control which allow you to configure it, e.g. SelectedItem
  3. 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.

Upvotes: 1

Related Questions