0

Is there any simple way to get a value of attached property in UWP? For instance I have a TextBlock defined in XAML:

<TextBlock
    x:Name="TextBlock"
    AutomationProperties.AutomationId="MyTextBlockId"
    Grid.Row="1"
    Text="My Text Block" />

If I want to get a value of property Text from string property name, I can use reflection:

TextBlock.GetType().GetProperty("Text").GetValue(TextBlock);

What about attached properties? How can I access attached properties Grid.Row or AutomationProperties.AutomationId based on their string "names"?

Of course I can do following:

AutomationProperties.GetAutomationId(TextBlock);
Grid.GetRow(TextBlock);

but I would like to use something like:

TextBlock.GetProperty("AutomationProperties.GetAutomationId");
TextBlock.GetProperty("Grid.Row");

UPDATE

DependencyPropertyDescriptor is not available in UWP.

Here seems to be the same question, however, it is not possible to use it in UWP.
Getting the attached property "Canvas.Left"

Community
  • 1
  • 1
Jakub Krampl
  • 1,774
  • 1
  • 16
  • 24
  • reflection? cant you just do `TextBlock.Text`? – M.kazem Akhgary Jan 23 '17 at 20:48
  • FYI attached properties why they are called attached properties after all? because other controls can use that property but that property does not belong to the control itself. for instance `TextBlock` does not have `Grid.Row` property, the fact is that `Grid` has attached property `Row` provided for other controls so other controls can use that property. – M.kazem Akhgary Jan 23 '17 at 20:52
  • 1
    Like this? http://stackoverflow.com/a/6291485/424129 -- `GetValue()`, `SetValue()`, same difference. Pass them the `DependencyProperty` – 15ee8f99-57ff-4f92-890c-b56153 Jan 23 '17 at 20:56
  • @M.kazemAkhgary I am aware of using `TextBlock.Text`, but question is about accessing attached properties from string. – Jakub Krampl Jan 23 '17 at 21:05
  • 4
    At the very least, you can use reflection to retrieve the field representing the attached property from the class that owns it (e.g. `Grid`, when dealing with `Row` or `Column`...you will need to know that class, obviously). Have you tried the WPF-based technique in the answer @Ed mentioned? What _have_ you tried? Please provide a good [mcve] that shows clearly what you've already tried and explain what _specifically_ you're having trouble with. Your question is too broad as it stands now. Most likely is a duplicate of some other, but without narrowing it, not even possible to know which other – Peter Duniho Jan 23 '17 at 22:01
  • @PeterDuniho `DependencyPropertyDescriptor` is not available in UWP. My question could be a duplicate of following [question](http://stackoverflow.com/questions/11535952/getting-the-attached-property-canvas-left), nevertheless, it is not possible do it the same way in UWP. However, I like what you wrote in your first sentence, I think that approach will be enough for my purpose. If you write it as an answer, I will accept it. Thank you. – Jakub Krampl Jan 23 '17 at 22:22

0 Answers0