WPF: Bind to each value of a property generated via reflection in ObservableCollection
I have a large list of objects, we'll call the object SpecialData, with ~60 properties on it. I created a class to represent each property in the UI like so (using MVVM toolkit hence the attributes):
In my viewmodel, I get all the attributes of the
SpecialData
class and map them to a collection of CheckableProperty objects like so:
Now in the UI, I want to show each property and its corresponding value for whatever my selected item is in the list of SpecialData
objects. Right now I am doing this every time I select a different item to show the properties of:
6 Replies
What I have doesn't seem like a good way to refresh the data when I change the selected item. Looking for any help to make it better.
Can't you just SG (Source Generator) it? Also faster than reflection
Have never done anything with source generators before, but I'm guessing that would just replace the reflection part? Would I still have to re-create the collection of properties every time I select a new object? Like if I listed out every property by hand in the xaml it would look something like
for every property
I'd rather use an
ItemTemplate
and just bind to something to auto-create it allIf you have never messed with source generators before it is quite a learning curve, it requires knowing the internals of Roslyn (compiler). But the best way is using a syntax tree visualizer, which I remember Visual Studio had.
And yes, it is just replacing the reflection part.
MVVM Toolkit uses source generators, that is why you need to use partial when applying the attributes.
Oh jeez, yeah never really looked into Roslyn related things. Maybe that'll be the next thing I read up on
Also know that you can create bindings by code
https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data/how-to-create-a-binding-in-code