开发者

Is it possible to know what setters are available in a style in wpf?

开发者 https://www.devze.com 2023-02-23 04:54 出处:网络
I have downloaded an assembly which has a class. The class exposes a style propert开发者_高级运维y to allow customizing appearance. Is it possible to know what are all the possible setter properties i

I have downloaded an assembly which has a class. The class exposes a style propert开发者_高级运维y to allow customizing appearance. Is it possible to know what are all the possible setter properties it will allow to create the style object? (eg., by looking at disaassmbly, etc.)

EDIT : if not possible, i am also looking for guidelines (eg., finding all dependencyproperty exposed by the class, look at MSIL code of the PutStyle etc.)


I didn't know exactly what your situation was when I read your question. After looking at it again, it sounds like you are trying to find on what properties can be set in your styles for an object. That would mean you would be interested in the non-readonly dependency properties that are available on a dependency object. Of course, there's the caveat that the object might not necessarily use all of the properties to render itself. So don't be surprised if you set some properties and nothing seems to change.

Just use reflection to search for all DependencyProperty fields and get the values (if the objects follow the conventions for declaring dependency properties). You could use this to get such properties.

public static IEnumerable<DependencyProperty> GetDependencyProperties(DependencyObject owner)
{
    var type = owner.GetType();
    var flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy;
    return type.GetFields(flags)
               .Where(fi => fi.FieldType == typeof(DependencyProperty))
               .Select(fi => fi.GetValue(null))
               .Cast<DependencyProperty>();
}

// get the non-readonly dependency properties
var writableDPs = GetDependencyProperties(myObject)
    .Where(dp => !dp.ReadOnly);


Jeff Mercado's suggestion sounds quite good, e.g. i just used this on a custom control:

var props = typeof(PanAndZoomControl).GetProperties()
    .Where(x => x.CanWrite)
    .Select(x => x.Name);
MessageBox.Show(String.Join(", ", props));

Is it possible to know what setters are available in a style in wpf?

I used a comma delimiter to make it more compact, you could just output that to a text file using File.WriteAllLines() or something like that.

0

精彩评论

暂无评论...
验证码 换一张
取 消