views:

43

answers:

1

So what I am trying to do is to have a set UI with certain controls, and I want to have some class instance to fill in the values.

For instance:

Classes that can populate the UI:

Sharpen, Blur, Smear, ... : FilterBase

So whenever the user creates an instances of the above classes, I want to automatically fetch the values based on the UI controls, by using the class instance.

The UI has items like:

.Name (TextBox)
.Amount (NumericUpDown)
.Decay (NumericUpDown)

So if the current instance is s = new Sharpen(), I want to get s.Name to fill out UI.Name.

How do I do this in the best possible way? Elegancy, clarity, performance wise, etc.

I am gonna use reflection, but how do I store the instance of the object (Sharpen, ...) and be type safe? Should I use an System.Object instead? Or T?

public partial class ParamsUI
{
    T CurrentFilter (?)
    ...
}

Also if I use T, how will I know what to cast it to?

Is there a better way?

+2  A: 

Since this is using Windows Forms, the most flexible option would probably be to just use the Windows Forms PropertyGrid directly.

You can do a lot of work to customize the look and feel of this. See Getting the Most Out of the .NET Framework PropertyGrid Control for details.


Edit:

If you want to have a very flexible, easy option, and WPF is an option (hosted within an ElementHost), you could consider using WPF.

With this, you could host a UserControl containing nothing but a resource dictionary and a ContentControl.

You could then make a custom XAML file for each item you want to edit, and setup a data template in the resources specifying the mapping of which user control (xaml) to use for each element you want to edit. You can then just set the element to the user control's "DataContext" in code, and it will automatically wire everything up and "just work".

Reed Copsey
Thanks Reed, will check it out.
Joan Venge
There's a better tutorial at http://www.c-sharpcorner.com/UploadFile/mgold/PropertyGridInCSharp11302005004139AM/PropertyGridInCSharp.aspx
Darwyn
@Darwyn: That tutorial is on using it, but not necessarily on customizing HOW your class displays. It covers a different subject than what I was after.
Reed Copsey