开发者

C# / WPF - Create UI to input parameters to method

开发者 https://www.devze.com 2023-02-17 21:57 出处:网络
Good Morning, I have a dynamic set of methods which all have the same return type ResultSet. I have the UI to display a ResultSet so what remains is to provide a UI for parameterizing the methods. Fo

Good Morning,

I have a dynamic set of methods which all have the same return type ResultSet. I have the UI to display a ResultSet so what remains is to provide a UI for parameterizing the methods. For example I might have the following methods;

public ResultSet FindNew (DateTime date, int id)
public ResultSet FindOld (DateTime date, int id)
public ResultSet FindMerged (DateTime date, int[] id)
public ResultSet FindNew (DateTime date, string name)

I want to use reflection to provide a drop down of all the methods with the correct return type within a class. Upon selection I would like to generate the input form required to parameterize the method. The user should then be able to submit the query and either see results or be informed of the error that occurred.

I think I can implement thi开发者_运维知识库s functionality in a naive fashion using my knowledge of C# but I was hoping there may be features of WPF that would help in my quest.

EDIT

Apologies that the question was too broad.

Given an arbitrary IEnumerable of ParameterInfo[] how would you bind to this in your View to create a form such that the user can enter a value for each parameter?

Specifically whilst not breaking MVVM if possible. The issues I can see are;

  • Rendering a ParameterInfo into a Label and a data entry component relevant to its type
  • Binding the data entry component to something in the ViewModel so that the value can be used

At the moment I'm considering using a custom type or Tuple of the ParameterInfo and the current value for that parameter. Then I could use DataTemplates to show the correct data input component (DatePicker Vs. TextBox Vs. ComboBox).

NB In winforms this was achieved through a PropertyGrid, there is an open source WPF project attempting to achieve the same but I'd rather use my own implementation due to the constraints of the project in terms of dependencies.


If your question is:

  1. Provided a list of functions with a similar return type (list may be modified)
  2. Construct an valid input control (or a collection of input controls) for the parameter types in each function
  3. Pass values collected in those input control(s) back to the call to each function
  4. Display result from each function

You are probably looking at too much dynamicism here. You can either construct your controls in code (which is probably not what you want, because you specified binding), or you can consider the following algorithm (if you have only one parameter to each function):

  1. For each function, construct an object. It must have a number of boolean properties, each of which maps to each type of input control. For example, UseCheckBox, UseNumericSpinner, UseIntArray, at most one of these properties can be true at any one time etc. Or you can use one Enum property. These properties should obviously be based on the parameter type of the function.
  2. In your XAML, you create a master items container to contain the functions
  3. Create a user control containing all possible types of input controls (numeric spinners, date pickers, text boxes, sliders etc.)
  4. Put this user control into your master items control's data template
  5. Bind each input control's visibility to the appropriate boolean property (e.g. CheckBox.Visibility should be bound to UseCheckBox). You may need to use a custom converter to convert boolean to Visilbility values. Or if you are using an enum property, bind to the enum value.
  6. Bind the list of objects to your data context of the items control

In this method, when you bind your list of objects to the items control, it will create a list of user controls, one for each function. The in each user control, the binding will automatically ensure only one input control is visible.


It is not at all clear what you want. Do you want to dynamically create input controls based on the parameters of a set of functions?

If that is the case, how I would have done it is:

  1. Have one class defined based on each parameter type(s).
  2. These classes should all implement an interface or inherit from a base class with a method that indicates the type of control to create.
  3. Your IEnumerable should contain a list of these classes
  4. You either dynamically create your controls by looping through these classes and instantiating a new input control based on each type, or
  5. You bind the collection to the DataContext of a items container element and the element's item template should contain a composite user control containing all possible forms of input controls, but only one is visible at any one time based on the type parameter; the value of the user control sould be the value of the visible input control
  6. When you get the values from all these input controls, pass them back to a common abstract Find method that accepts an object, but do type checking in each class's override implementation
0

精彩评论

暂无评论...
验证码 换一张
取 消