vendredi 22 février 2019

wpf, c# set Margin.Left of FrameWorkElement using Reflection

Does anyone know how to set a second-level property using reflection? In case I want to set the width of lets say a StackPanel, that works fine:

        PropertyInfo pi = stp.GetType().GetProperty("Width", BindingFlags.Public | BindingFlags.Instance);
        if (null != pi)
        {
            pi.SetValue(stp, Convert.ChangeType("100", pi.PropertyType), null);
        }

But if I want to set Margin.Left:

        PropertyInfo pi = stp.GetType().GetProperty("Margin.Left", BindingFlags.Public | BindingFlags.Instance);
        if (null != pi)
        {
            pi.SetValue(stp, Convert.ChangeType("100", pi.PropertyType), null);
        }

It doesn't work at all. pi is null. I can't get a valid PropertyInfo. Trying to get a valid FieldInfo fails also:

        FieldInfo prop = stp.GetType().GetField("Margin.Left", BindingFlags.Public | BindingFlags.Instance);
        if (null != prop)
        {
            prop.SetValue(stp, Convert.ChangeType("20", prop.FieldType));
        }





Aucun commentaire:

Enregistrer un commentaire