Auto generate textboxes for properties with public set C# WPF - c#

I want to be able to bind a listbox, Treeview or some control to a ObservableCollaction>SomeObject>() and then auto generate textboxes to all properties with a public set within SomeObject. That way I dont have to add or remove any code in WPF if someone remove or add a new property to SomeObject.
Can anyone please point me in the right direction or tell me if this is a stupid idea?
//Max

I don't think you should use ObservableCollection for that. That's used when you want to present a collection that will be modified in runtime (i.e. having elements added, removed or modified). Do you expect SomeObject properties names to be modified at runtime?
If you just want to expose the names of the properties and their values, use Reflection. Take a look at this. Add some behind code to create a collection of pairs with names and values. That should do the trick
Then use some DataTemplate to create the textboxes from this info. I find this tutorial very useful

I used this to solve my problem

Related

How to remove properties that don't have a setter from properygrid c#

I am making an app in Winforms where the user selects an object from a list (all of which are different classes) then edit its properties. Some of the objects have properties greyed out because they don't have a setter. Is there a way to automatically remove those properties from the grid, or get a list of all entries like that and remove them programmatically.
Thank you.
I figured out how to do it. Just use PropertyTab and filter out the properties when it collects them.

Displaying ListviewItem Properties In Grid Columns

So I've added a bunch of items to a ListView. All of the items added are structures that have 4 members. The members are Amount, Location, Date_Time, and Category. These members are setup as properties and I was trying to bind each member to a column in the ListView.
Unfortunately, my results are blank entries or the added structures ".tostring()" return value. Essentially I have no Idea how to correctly setup up the binding. Any ideas in XAML or C# would be appreciated. Also the structure resides in a separate class, not the MainWindow class that contains all the controls. Formatting and styling is still in the works for my listview so don't judge my bad color scheme.
Thanks
Don't use images to show us your code. StackOverflow allows to insert code snippets. Also use paragraphs to format your question. Always see on your question after you've posted it.
Binding works only with public properties. Change access modifier of your structure and its properties from internal to public. See answer at this topic.

Dynamically change XAML binding based on type

I have a list of objects that is being used as the source for a DataGrid. This list of objects is a base class type, of which there are 2 or more inherited types. I am trying to bind to properties of the base class to display as values in the columns of the DataGrid. The subclasses have different properties available to them which I would like to be able to display, so my question is, does XAML have a way to dynamically change the value of a binding based on the type that is being pointed to? I have thought about potentially doing this with a converter, but if I understand correctly, then I would have to write a different converter for each subclass property that I need to bind to. Any advice or suggestions are appreciated. Thanks
This is probably possible, with attributes and some reflection you could mark the properties that should go in the various columns and then auto-generate those. You could also create a sparse grid, with columns for all possible properties; this should also be possible via reflection and does not require any additional metadata.
(You can auto-create the columns based on the items using an attached property (like this) if the native DataGrid event is not sufficient for this.)
You can define multiple DataTemplates and specify the intended type through the DataType property. The correct template will be selected depending on the type of the bound object.
https://msdn.microsoft.com/en-us/library/system.windows.datatemplate.datatype(v=vs.110).aspx
The means to achieve this would be a CellTemplateSelector.
See this link.

Customizing the FrameworkElements used to display the contents of an ObservableCollection

So it seems like what I want to do should be straightforward, but I haven't been able to find a way to do it...
I need to display a list of objects that represent custom elements for entering data. Exactly how each object is displayed depends on the parameters of the object - so it could be a grid containing a name, description, and text box. It could be a grid with a couple labels and a dropdown. It could be an expander that contains multiple sub-objects. It could be something new that hasn't been built yet (so it needs to be extensible). Right now, I am populating this list by creating the FrameworkElement for each object and then adding it to a Grid by hand.
I would like to switch to keeping my objects in an ObservableCollection, and then binding that collection to a ListBox (or similar). That way, when new objects are added or removed from the list, the UI would automatically update itself accordingly. What I can't figure out is, is there a way to point it to my C# method for creating the custom-configured FrameworkElement for each object, so that when new objects are added the appropriate elements will be added to the UI?
Well, you're on the right track as far as wanting to use an ObservableCollection<T> and a ListBox control. Though, I'd venture to say you might want to simply use an ItemsControl since you probably don't care about selecting a particular item, but merely displaying an enumeration of items, and the ListBox would allow you to actually select one of those items.
Your problem is that you want each item in the list to display differently depending on certain criteria. For this you'll want to look at the DataTemplate and DataTemplateSelector classes.
Basically, a DataTemplate is a way of saying "I want my item to look like this.", and a DataTemplateSelector is a way of saying "I want to select this specific DataTemplate based on this criteria."
Here are a few examples on how to use the DataTemplate/DataTemplateSelector classes:
http://tech.pro/tutorial/807/wpf-tutorial-how-to-use-a-datatemplateselector
http://mikestedman.blogspot.com/2009/04/datatemplateselector.html
http://wpftutorial.net/DataTemplates.html
Seperating the presentation from the model is always a good idea. It seems like you are on the right track.
Foreach object type, you should create a DataTemplate and then use ItemTemplateSelector to select the correct template for each object type.
Good luck

WPF check if a dataproperty has an binding

Hej,
I'm trying to make som general functionality for my ListView, so that the content of a ListView can be exported to CSV directly.
I'm trying to achive this by getting the datacontext and analysing the ICollectionView for this. From here I have access to the all the objects from via ICollectionView via SourceCollection, in which I (for now) presume sorting/and filtering is respected.
The challange here is that I only want to output the columns that also are showed in the ListView.
When iterating my collection, is there a function where I can evluate if a property in a class (with notification suppoert) has a binding to it?
The esiest solution for now would be just to output all properties, but I'm not interested in this, since oid's are not fun to look at.
Thx in advanced.
/Ian
I suggest you recognize that the ability to determine which data is being displayed is a business requirement. Thus, you should embody this requirement in your model. In other words, your model should clearly indicate which columns are visible - you shouldn't be trying to infer this from your existing properties, nor should you be examining your view.
There are a whole bunch of ways you could do this, but the key is to have this information on hand in your model.
Why don't you just look at the DataTemplate and evaluate it including the binding inside?
There's no easy way to do it... You can check the binding on the target side (dependency property), but not on the source side.
For what you're trying to do, you could loop through the columns of the ListView and check their DisplayMemberBinding, but it could be undefined (the cell content might be defined using the CellTemplate property instead).

Categories