Is it possible to dynamically get the binding properties for a view into a class and manipulate it in run time.
for example, if the DataContext
is of type User
that has 10 properties, but the bound properties are only 4, can I make a class that has these 4 properties only?
Asked
Active
Viewed 124 times
1

mshwf
- 7,009
- 12
- 59
- 133
-
It is possible. What exactly is the goal you are trying to achieve here though? – MrZander Sep 27 '17 at 21:14
-
I want to serialize the data appearing in the view, and I want to get it as short string as I can, so I want to exclude the unbound properties – mshwf Sep 27 '17 at 21:16
-
sometimes the DataContext class has dozens of properties and collections, but what is shown in a specific view just few properties, so serializing this mini-class would save me a lot – mshwf Sep 27 '17 at 21:18
-
Ok. I see what you mean now. I am not sure if that is possible in the way you want. – MrZander Sep 27 '17 at 21:18
-
If you have it working - post your code, please. Would be very useful! – Maciek Świszczowski Sep 27 '17 at 22:05
-
Hi Mohamed, did you solve your problem? – Maciek Świszczowski Oct 10 '17 at 20:49
1 Answers
0
Doable, but difficult.
- You must parse the visual tree, and visit every control using VisualTreeHelper (see: https://stackoverflow.com/a/874426/275330).
- For every control you can get a list of its dependency properties (see https://stackoverflow.com/a/26367132/275330)
- Then for each separate dependency property you can check if its value is set locally (see: Knowing if a DependencyProperty has not been set in XAML). If so - you have the value you need.
I've never used 1+2+3 together, so I don't know what additional difficulties you may have.

Maciek Świszczowski
- 1,155
- 11
- 28