How to get DependencyProperty from PropertyName in Runtime

BitSmithy 1,751 Reputation points
2024-03-28T16:03:13.4033333+00:00

Hello,

How to get DependencyProperty from PropertyName in Runtime.

I want to write such code:

TextBox tbx = new TextBox();
tbx.SetValue(TextBox.TextProperty, "ssss");

more gerneral. I want to do something like that:

TextBox tbx; tbx.SetValue(GetDepenedencyPropertyFromPropertyName("Text"), "aaaaa")

Function

DependencyProperty GetDepenedencyPropertyFromPropertyName("Text") {}

should work in Release (not only in debug mode) mode. Is it possible to do such function in UWP.

Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Junjie Zhu - MSFT 14,831 Reputation points Microsoft Vendor
    2024-03-29T02:38:38.7233333+00:00

    Hi @BitSmithy ,

    Welcome to Microsoft Q&A!

    I think GetDepenedencyPropertyFromPropertyName is not a feasible method to find the correct DependencyProperty based on string, because it also needs to confirm the type of the control.

    But even so, I still didn't find a suitable API to get the DependencyProperty in UWP. It is recommended that you use your original code tbx.SetValue(TextBox.TextProperty, "ssss");

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful